Addition of some swap space is a great alternative for increasing the responsiveness of your cloud server. Swap is that space of the hard drive which can be utilized for transitorily holding that information which typically reside in RAM. This results in the availability of increased RAM for your processes that are running currently.
What Goes On Inside?
The information is stored by Linux kernel within RAM in the form of small chunks called “pages”. RAM’s space is consumed by the number of pages that a program has. New pages are generated as and when any new process requires access to the physical memory. But in case RAM does not have more space for new pages, then what will happen?
Suppose a program does not utilize all the pages allotted to it in RAM, then those remaining pages can be deleted and that area in RAM can be allotted again. It is easy as if the need arises for those page files then they can easily be recreated. For instance, if 5 pages have been allocated to program one, but it writes to only 3 pages then the remaining 2 pages can be eliminated temporarily and can be recreated in case they are ever needed. Meanwhile, the free space of those 2 pages can be utilized by other programs.
So what is swap’s utility?
The system sometimes needs to eliminate pages from RAM that contain data which can not be regenerated as desired. They contain information that is not available readily on the hard drive. Now, when pages in such state are removed by the system, then it requires some place for putting them safely so that information doesn’t get lost. “Swap” is that place.
A swap partition or a swap file does not contain anything complex. It simply refers to a space that’s reserved for briefly storing RAM’s data, including cached data, data from applications, or anything else occupying RAM. Depending upon the necessity, OS swaps information from swap area to RAM and vice versa.
Checking for Swap Space
For following the steps in this guide, log in as root.
Prior to creating your own swap file, one needs to check whether a swap space has been enabled on the system or not. A number of methods are there for checking it. We will first try the “swapon” command.
swapon: stat failed -s: No such file or directory
This command will not generate any output over a clean Arch Linux VPS. Even after correct execution of command, no swap space is found.
Now we will try another method of checking swap that will generate more visual feedback.
total used free shared buffers cached Mem: 494 60 434 0 6 28 -/+ buffers/cache: 25 469 Swap: 0 0 0
As we had expected, 0 is the output of swap’s total column. This reaffirms “swapon” command’s result.
Checking the File System
A number of beliefs are there about the perfect size of a swap space. Generally, it’s size is kept equal to or twice the amount of RAM the user has ( except in those cases where extremely large RAM size is there and in those cases, lesser swap area is allotted). Finally, it boils down to your personal choice, the availability of the hard drive space and the amount of RAM in your system.
For this tutorial, we have 20GB hard drive and 512MB of RAM. Suppose we setup our swap area to be double in size of our RAM space , then a major chunk of our hard drive would be taken up by swap space. Therefore, we will consider creating the swap space to be equal to 512MB or the amount of RAM that we have.
Prior to the actual creation of swap space, we have to check that enough space is available on our hard drive. We can check this by using “df” command.
Filesystem Size Used Avail Use% Mounted on /dev/vda 20G 965M 18G 6% / dev 240M 0 240M 0% /dev run 248M 212K 248M 1% /run tmpfs 248M 0 248M 0% /dev/shm tmpfs 248M 0 248M 0% /sys/fs/cgroup tmpfs 248M 0 248M 0% /tmp
Looking at the first line, it is clear that out of our 20GB hard drive, roughly 18GB is unused. So we have enough space for creating our swap space of 512MB.
Creating and Enabling the Swap File
Now, we are ready for creating our swap space. Two different methods will be used for accomplishing it. Firstly, we will be using the “dd” command.
dd if=/dev/zero of=/swapfile bs=1M count=512
512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 1.96268 s, 274 MB/s
Using the command given below, we can ensure that the file was correctly created.
ls -lh /swapfile
-rw-r--r-- 1 root root 512M Jun 28 18:41 /swapfile
It can be seen from above that we now possess a swapfile of 512MB.
Now we will try to create swap file through a different method. First we will eliminate the swap file that was created just now so that we can again try.
The “fallocate” program can be used for creating the swap files much faster than the “dd”command. Additionally, its syntax is much simpler to remember.
fallocate -l 512M /swapfile
We can again view that a new swap file of the exact size has been created.
ls -lh /swapfile
-rw-r--r-- 1 root root 512M Jun 28 18:54 /swapfile
Also, a file named “swapfile” is there in our hard drive’s root partition, but it is also visible that the system is neither recognizing nor using it.
total used free shared buffers cached Mem: 494 41 453 0 1 13 -/+ buffers/cache: 25 468 Swap: 0 0 0
At present, it is only a regular file. We can call it as “swapfile”, but the Linux system still does not know how to utilize it as swap. For this, we need to enable it following formatting it as swap.
But before that, very crucial step needs to be completed. Firstly, the file permissions needs to be changed so that read and write to the file can only be performed by the root. It poses a huge security vulnerability if non-root users are allowed to write to or read from a swap file. You previously saw that when we ran “ls”, the file was readable by anyone on the machine. We will now change that.
chmod 600 /swapfile
On re-checking the file, it can be seen that now correct permissions are set.
ls -lh /swapfile
-rw------- 1 root root 512M Jun 28 18:54 /swapfile
Since our file is now secure, we will start formatting it.
Setting up swapspace version 1, size = 524284 KiB no label, UUID=61f42c98-d1ef-4f27-84c2-713c5f63d434
Lastly, we are going to enable the file for the system to understand and start using it.
Now we will check if our file is being recognized and used by the system.
total used free shared buffers cached Mem: 494 42 452 0 1 14 -/+ buffers/cache: 25 468 Swap: 511 0 511
Excellent! Now our swap file has been recognized. The OS will now start utilizing it for storing RAM’s data as and when required.
Although, we aren’t finished yet. The OS needs to be told that this file is safe enough to be used for swapping every time it boots up. The “/etc/fstab” file can be edited for accomplishing this.
# Paste this information at the bottom of the file /swapfile none swap defaults 0 0
Tweaking of Swap Settings
A few things need to be kept in mind while using swap. Swap area is comparatively slower than RAM as it utilizes a portion of hard drive. Thus, you will want it to be used by the OS only when it is absolutely necessary. A massive slowdown in operations may be experienced if the pages are moved to your swap space before your RAM is out.
The parameter called “swappiness” controls how frequently will the items be moved out of RAM and within a swap area. It has a value from 0 and 100. If values are closer to zero, then the Linux kernel will not swap until it is an absolute necessity. If the values are closer to 100, then the files will be swapped out much earlier. However, speed issues may arise due to this as information is being brought from a hard drive and not RAM.
The command given below can be typed in for finding out our existing swappiness setting:
The default value of swappiness for our system is 60, a good default value for a VPS. In case we were modifying a desktop’s swappiness, probably we would move it nearer to 0. Different values of swappiness can be tried by utilizing the “sysctl” command.
vm.swappiness = 10
It can be seen that the swappiness parameter’s new value has been accepted by the system.
In order to set this as default, we need to edit the “/etc/sysctl.conf” file.
# Search for the vm.swappiness setting. Uncomment and change it as necessary. vm.swappiness=10
This permits our settings to sustain through reboots. You can do hit and try with this parameter for obtaining optimal swapping suitable for your situation.
“vm.vfs_cache_pressure” setting is one more related parameter that we need to adjust. By modifying the value of cache pressure, you can alter the preference of the system to keep inode and dentry cached information over all other types of information. This translates to OS retaining information about the file system for longer time, that is requested frequently and is very expensive to find. Usually, this leads to a gain in performance.
Once more, the current value can be checked by looking in the /proc directory.
The existing settings transfer inode information more quickly out of cache than desired. A cache pressure of 50 would serve our purpose. These settings can be changed in the same method as were done for swappiness.
vm.vfs_cache_pressure = 50
Once more, we can permit our settings to sustain through reboots by adding it to the “/etc/sysctl.conf” file.
# Search for the vm.vfs_cache_pressure setting. Uncomment and change it as necessary. vm.vfs_cache_pressure = 50
Now our cache pressure configuration is made permanent.