If you have worked with Linux, you must have come across “Linux swap“, especially during the installation process of most Linux distros. In this article, we would be taking a look at Linux Swap and why is it necessary.
What is Linux swap and why do we need it?
If you are an ardent Linux user, you know that Linux Swap has something to do with RAM. Well, swap actually serves as an extra storage in case your RAM is too full and is overflowing. In a scenario where the RAM cannot accommodate any more new processes, new applications can run off Swap space. However, such processes will be much slower than RAM.
The Linux Kernel moves the memory pages which are hardly ever used into the Swap space so as to free up RAM which can then be utilized by more frequently accessed memory pages. The “swappiness” value decides which pages would be retained in memory and which would be sent to Swap space. Lower the swappiness, lower are the chances of data being moved to the Swap partition.
It is a good practice to have a Swap partition or Swap file even with systems with a high RAM. It moves hardly used pages from cache to memory to boost performance. For systems with comparatively lower RAM as it might happen that the RAM runs out of space for new processes and fail to allocate memory for new processes. In some extreme cases, the Kernel will deploy OOM killer to nuke high-memory processes.
How much swap space do we need?
The amount of Swap that should be allocated is directly proportional to the amount of RAM we have on our system. The recommended settings are :
|Size Of RAM||Recommended Size Of Swap|
|< 1GB||Same as that of the RAM|
|2GB – 4 GB||Half The Size Of RAM|
|> 4GB||4GB Of Swap Should Be Enough|
We can check for the type and size of our current swap with :
NAME TYPE SIZE USED PRIO /dev/sda2 partition 4G 0B -2
Thus, we have a swap of 4GB enabled.
How to enable Linux swap?
It is mostly recommended to setup a swap partition during the installation of your OS. However, you can also create a swapfile instead of a dedicated partition. A swapfile has the added advantage that you can change it’s size as per requirements.
To create a swap partition, use :
$ sudo fallocate -l 4G /swapfile
This creates a swapfile of 4GB, but you can choose whatever size you deem fit.
Next we need to set the proper permissions with the chmod command:
$ sudo chmod 600 /swapfile
Finally we can enable swap with :
$ sudo swapon /swapfile
Finally, we need to update our fstab to make the changes permanent. To do so, add the following line to /etc/fstab :
/swapfile swap swap defaults 0 0
After this, we should have swap enabled !
Remove a swap file
To disable swap, first deactivate it with:
$ sudo swapoff -v /swapfile
After that, remove the previously created entry from /etc/fstab. Finally, remove the actual swap file with :
$ sudo rm /swapfile
How do we change swappiness?
Swappiness dictates to which extent is the swap used. The default swap value is 60. The swappiness value is inversely proportional to the amount of RAM used.
To verify your swappiness value do :
$ cat /proc/sys/vm/swappiness
To modify the default value, we need to edit the /etc/sysctl.conf file and add the following at the end of the file :
10 is the most recommended value of swappiness but you can change it accoringly.
Save and exit. For the changes to take effect, we need to reboot or system after which our system will use more of RAM and less of Swap.
Thus we saw that Linux swap can be quite instrumental, especially for older machines. Sometimes, it is advised to have swap enabled even for machines with sufficient RAM just as a safe practice.