Swap space is a part of the hard disk that is used when the RAM  memory is full. The swap space can be a dedicated swap partition or a swap file.

When a Linux system runs out of physical memory, inactive pages are moved from the RAM to the swap space.

Swappiness  is a Linux kernel property that sets the balance between swapping out  pages from the physical memory to the swap space and removing pages from  the page cache. It basically defines how often the system will use the  swap space.

This article explains how to change the swappiness value on Linux systems.

Checking the Swappiness Value

To check the current swappiness value on your system, use the following cat command:

cat /proc/sys/vm/swappiness

The default swappiness value on most Linux distributions is 60:

60

While the swappiness value of 60 is appropriate for most users, in some cases, you may need to set a lower value.

Another command that you can use to determine the swappiness value is sysctl:

sysctl vm.swappiness
vm.swappiness = 60

Changing the Swappiness Value

Swappiness  can have a value between 0 and 100. A value of 0 instructs the kernel  to aggressively avoid swapping out for as long as possible. A value of  100 will aggressively be swapping processes out of physical memory.

A  lower value will make the kernel to try to avoid swapping whenever  possible while a higher value means the kernel will try to use the swap  space more aggressively.

Accessing swap memory is much slower than  accessing physical memory directly. A lower value for the swappiness  parameter will most likely improve overall system performance. For  regular desktop installation, a value of 10 is recommended. A swappiness  value of 0 or 1 is recommended for most database servers.

The optimal swappiness value depends on your system workload and the size of the RAM memory. You should adjust this parameter in small increments to find an optimal value.

For example, to set the swappiness value to 10 at runtime, type the following command as root or sudo user:

sudo sysctl vm.swappiness=1

To make the swappiness parameter persistent across reboots open the /etc/sysctl.conf file with your text editor:

sudo nano /etc/sysctl.conf

Locate the vm.swappiness parameter and change its value. If this parameter does not exist, append the following line to the file:

/etc/sysctl.conf

vm.swappiness=1

Conclusion

We have shown you how to change the value of the swappiness parameter.

If you have feedback, leave a comment below.