How to Change the Swappiness Value in Linux?

In Linux, the “swappiness” parameter is a kernel setting that controls the balance between swapping out runtime memory to the swap space (part of the hard drive). All the swap operations are performed between the hard drive and RAM.

The content of this guide is to demonstrate various methods to change the swappiness value in Linux. The supported content of this article is given below:

Let’s start the article with the “swappiness” value.

Purpose of Changing Swappiness Value

The swappiness value is required when the RAM is full and demands memory space from the hard drive. The value is set to 60 (threshold) on most systems. It represents that the kernel will start swapping out idle memory to the swap space when the free RAM falls below a certain threshold. The threshold is computed based on the total amount of RAM and the swappiness value.

Display the Current Swappiness Value

You can view the current swappiness value by accessing the “/proc/sys/vm/swappiness” file. For instance, the following command will display the current swappiness value:

$ cat /proc/sys/vm/swappiness

The output shows that the current swappiness value is set at “60”.

Change the Swappiness Value

You can change or modify the swappiness value by writing a new value to the “/proc/sys/vm/swappiness” file. To set the swappiness value to 80, first access the particular file:

$ sudo nano /proc/sys/vm/swappiness

It navigates to the “swappiness” file via the nano editor. There, change the swappiness value from 60 to 80. After modification, save the file using “Ctrl+S” and the editor through the “Ctrl+X” shortcut key.

Note: It is not recommended to set the swappiness value to 0, as this can cause the system to run out of memory and crash.

While setting the value to 100 can cause the system to swap aggressively.

How to Permanently Change the Swappiness Value?

To make permanent changes, modify the “/etc/sysctl.conf” file by adding the swappiness value:

$ sudo nano /etc/sysctl.conf

It navigates to the “nano” editor. After that, adds the swappiness value as displayed in the below script:

$ vm.swappiness = 40

Save the file using the “Ctrl+S” key and exit the editor via the “Ctrl+X” key.

That is how you can change the swappiness value.

Conclusion

To change the swappiness value in Linux, access the “/proc/sys/vm/swappiness” file and set the new swappiness value (recommended between 40 and 80). This changed swappiness value will apply to the operating system for switching between RAM and hard disk memory. This guide has explained all possible ways to change the swappiness value in Linux.