Linux Swap Partition/File for Paging

The Linux Kernel divides RAM into chunks of memory. Swapping takes place when the Linux Kernel uses a hard disk space (swap space) to store information from RAM and thus releases some RAM space. That is why when you install a Linux distribution, the installation wizard usually asks you to assign some space for the system and some other for the swap.

Using swap is a very useful way to extend the RAM because it provides the necessary additional memory when the RAM space has been exhausted and a process has to be continued. It is especially recommended when you have very little RAM.

Do you need Linux Swap?

This is a question many novice users ask themselves when they begin to discover Linux. In fact, this will depend on the use and amount of RAM your computer has. Regarding the use, there are processes and applications that really use a lot of memory, for example, Google Chrome. However, most of the current equipment comes with at least 8GB of RAM and that makes the swap process less necessary. Nevertheless, having a swap space is desirable even if you have a lot of RAM.

For example, usually, when your RAM gets full and the Linux kernel has no space to write into, your system will crash. On the other hand, if you have some swap space, it will be used by the Linux kernel and your system will keep working, though much slower. So, it is safer to have swap space.

Note: swap space has one disadvantage - it is much slower than RAM. So, adding a swap space will not make your computer faster, it will only help to overcome some limitations posed by RAM size.

Linux Swap Partition

I recommend creating a swap partition during the installation of your Linux distribution.

Generally, these are the recommended sizes for the swap partition.

Still, everything depends on your use case.


You can check the type and size of your swap with this command:

swapon

Linux Swap File

Alternatively, you can create a Linux Swap File after the installation. The modern Linux Kernel allows Swapping to a swap file instead of a swap partition. A swap file has an advantage over a swap partition that you can change the size of your swap any time easily by changing a swap file size.

If you want to create a swap file, run this command first:

sudo fallocate -l 1G /swapfile

Note: this command is to create a 1GB swap file. Replace 1G with the value you want.

Next, you have to set the correct permissions:

sudo chmod 600 /swapfile

Then, format the file to swap:

sudo mkswap /swapfile

Finally, enable the swap:

sudo swapon /swapfile

Yet, if you want the changes to be permanent, you need to edit the /etc/fstab file and add the following:

/swapfile swap swap defaults 0 0

Finally, check the status of the swapfile:

sudo free -h

How to remove a Linux Swap File

In case you need to remove a Linux swap file for any reason, you need to follow these steps.

First, deactivate the swap:

sudo swapoff -v /swapfile

If you created the entry in the /etc/fstab file, remove it. To remind you, it is a line much like: /swapfile swap swap defaults 0 0 entry

Finally, delete the actual Linux Swap File.

sudo rm /swapfile

How to adjust the Swappiness value

Swappiness is a property of the Linux Kernel to define how often the swap space will be used. As you know, RAM is faster than a hard drive, therefore every time you need to use swap, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance.

Normally, the default swappiness value is 60. The smaller this value, the more of your RAM will be used.

To find the swappiness value, run this command:

cat /proc/sys/vm/swappiness

You should see the value of 60.

If you want to modify the default value, you need to edit the file /etc/sysctl.conf.

sudo vi /etc/sysctl.conf

and add or change the following line (10 is the most commonly recommended value):

vm.swappiness=10

In order for the changes to take effect, you need to reboot the system.

This way your Linux kernel will use more RAM and less swap, but it still will swap when your RAM memory gets critically full. Usually, this setting is recommended when you have more than 4GB of RAM.