How to add a Swap partition on Ubuntu 16.04, 16.04 swap
Preface
One of the simplest ways to increase server response speed and prevent application memory insufficiency errors is to add some swap space. This Guide describes how to add swap files to the Ubuntu 16.04 server.
Note:
Although swap areas are generally recommended for systems that use traditional hard drive rotation, switching to SSD may cause hardware degradation over time. For this reason, we do not recommend that you enable swap partitioning on DigitalOcean or any other vendor that uses SSD storage. This may affect the reliability of the underlying hardware of you and your neighbors. This guide is only for reference by users who may use a rotating disk system elsewhere. If you need to improve the performance of the DigitalOcean server, we recommend that you upgrade your Droplet to have a better experience and reduce the possibility of hardware problems that affect your services.
1. First, let's take a look at what is Swap
SwapPartition (also called swap partition) is an area on the hard disk. It is specified as a place where the operating system can temporarily store data, and the data cannot be stored in RAM. Basically, this allows you to increase the amount of information the server retains in the "Memory" of the work, but there are some precautions, the swap space on the hard drive is used when RAM does not have enough space to accommodate the application data being used.
Writing data to a disk is much slower than storing data in RAM, but the operating system is more willing to store application data in the memory and use swap old data. In general, when the system's RAM is used up, using swap space as a drop-down space may be a good safety net, which can prevent non-SSD storage systems from insufficient memory.
2. Check System exchange information
Before we start, we can check whether the system has some available swap space. There may be multiple swap files or swap partitions, but it should usually be enough. You can run the following command to check whether the system has swap partitions:
$ sudo swapon --show
If there are no results or no display, there is no swap space available for the system. You can also use the free tool to verify that no swap partition is available currently.
$ free -h
Output result:
total used free shared buff/cache availableMem: 488M 36M 104M 652K 348M 426MSwap: 0B 0B 0B
We can see that the "swap" row here is 0, that is, there is no switch in the active state on the system.
3. Check the available space on the hard drive partition
The most common way to allocate space for swap is to use a separate shard for a specific task. However, it is not feasible to change the partition scheme, we just can easily create swap files residing in existing partitions.
Before starting, run the following command to check the current disk usage:
$ df -h
Output result:
Filesystem Size Used Avail Use% Mounted onudev 238M 0 238M 0% /devtmpfs 49M 624K 49M 2% /run/dev/vda1 20G 1.1G 18G 6% /tmpfs 245M 0 245M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 245M 0 245M 0% /sys/fs/cgrouptmpfs 49M 0 49M 0% /run/user/1001
In this case, the device under/dev is our disk. In this example, we have enough space (only 1.1 GB). Of course, your usage may be different.
Although you have many comments on the proper size of the swap space, it depends on your personal preferences and application requirements. Generally, it is equivalent to double or double the amount of system memory, which is a good starting point. Another good experience is that if you just use it as a RAM slave, the size of the swap partition should not exceed 4 GB.
4. Create a swap file
Now that we know the available hard disk space, we can create a swap file in the file system. We will create a file named swapfile under our root (/) directory. The best way to create a swap file is to use the fallocate command, which can immediately create a pre-allocated file. Since the server RAM size in this example is 512 MB, we will create a 1 GB file in this tutorial and adjust it as appropriate, to meet your server needs:
$ sudo fallocate -l 1G /swapfile
After creation, we can use this command to verify whether the correct swap space is retained:
$ ls -lh /swapfile
Display result:
$ -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile
This indicates that the correct space size has been created for our file.
5. Enable swap file
Now we have a 1 GB file, and we need to convert it into a swap space.
First, we need to lock the File Permission so that only users with root permission can read the file content, which can prevent common users from accessing the file, so as to avoid major security risks.
Root permission of the object to be locked:
$ sudo chmod 600 /swapfile
Verification permission:
$ ls -lh /swapfile
Display result:
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
As you can see, only the root user has enabled the read/write flag.
Next, we can use the following command to mark the file as a swap space
$ sudo mkswap /swapfile
Display result:
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
After marking the file, we can enable the swap file so that our system can use it:
$ sudo swapon /swapfile
Run the following command to verify whether the swap space is available:
$ sudo swapon --show
Display result:
NAME TYPE SIZE USED PRIO/swapfile file 1024M 0B -1
At this time, we can view our settings again through free:
$ free -h
Display result:
total used free shared buff/cache availableMem: 488M 37M 96M 652K 354M 425MSwap: 1.0G 0B 1.0G
As you can see, the swap partition has been successfully created and the size is 1.0 GB. The operating system will use it if necessary.
6. Retain swap files permanently
We recently enabled the swap file for the current session. However, if we restart, the server will not automatically retain the swap settings, we can change this by adding the swap file to the/etc/fstab file.
Back up the/etc/fstab file to prevent errors:
$ sudo cp /etc/fstab /etc/fstab.bak
Add the swap file information to the end of the/etc/fstab file:
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
In this way, the swap file is retained.
7. Adjust your swap settings
Several options can be configured during exchange processing. These options will affect the system performance.
7.1 adjust swappiness attributes
The swappiness parameter configures the frequency at which your system switches data from RAM to the swap space. The value is between 0 and 100, indicating the percentage. If the swappiness value is close to 0, the kernel will not swap data to the disk unless absolutely necessary. Remember that interaction with swap files is "expensive" because it takes longer to interact with swap than with RAM and causes a significant reduction in performance. The system is less dependent on swap partitions, which usually makes your system faster. A value close to 100 of swappiness will try to put more data into the SWAp to keep more RAM space. Depending on the memory configuration file of your application or the server you are using, this may be better in some cases.
View the current swappiness value:
$ cat /proc/sys/vm/swappiness
Result Display
60
For a desktop system, the swappiness setting of 60 is not a bad value, but for a server, you may want to set it closer to 0.
We can use the sysctl command to set swappiness to a different value. For example, to set swappiness to 10:
$ sudo sysctl vm.swappiness=10
Display result:
vm.swappiness = 10
This setting will be retained until the next restart of the system. If you want to take effect after the restart, you can add a line in the/etc/sysctl. conf file:
$ sudo nano /etc/sysctl.conf
Add at the end of the file:
vm.swappiness=10
Save and close the file.
7.2 adjust cache pressure settings
Another related value you may want to modify is vfs_cache_pressure. This setting configures how much data will be cached by the system inode and dentry information. Basically, this is access to the data about the file system, which is usually time-consuming queries and frequent requirements. Therefore, this is a good thing for your system to cache, you can query the proc file system again to view the current value.
$ cat /proc/sys/vm/vfs_cache_pressure
Output result:
100
This configuration may cause our system to delete inode information from the cache too quickly. We can set a more conservative value, such as 50.
$ sudo sysctl vm.vfs_cache_pressure=50
Display result:
vm.vfs_cache_pressure = 50
Similar to swappiness, this is only valid for the current session. We can change it by adding it to our configuration file, just as we use our swappiness settings:
$ sudo nano /etc/sysctl.conf
Add at the end:
vm.vfs_cache_pressure=50
Save and close the file.
8. Summary
Following the steps in this guide will give you some breathing space, otherwise it will lead to exceptions with insufficient memory. swap space is very useful to avoid these common problems. If an OOM (insufficient memory) error occurs, or if you find that the system cannot use the required applications, the best solution is to optimize the application configuration or upgrade the server.