How to change the Swappiness of a Linux system

What is Swappiness?

Most Linux users who have previously installed a distribution must notice the presence of “swap space” during the partitioning phase (usually found in / sda5). This is a dedicated space in the hard drive, which is usually set to at least twice the amount of RAM and together with it constitutes the total virtual memory of the system. The Linux kernel sometimes uses this swap space by copying blocks from RAM to swap, allowing active processes to require more memory than is actually available.

Swappiness is a kernel parameter that defines the amount (and frequency) that the Linux kernel will copy the contents of RAM for exchange. The default value of this parameter is “60” and it can take any value from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively the kernel will swap.

Why change it?

The default is a one-size-fits-all solution, and efficiency may vary in all individual use cases, hardware specifications, and user needs. In addition, the interchangeability of the system is the main factor that determines the overall function and speed performance of the OS. That said, it is important to understand how interchangeability works and how the various configurations of this element improve the operation of your system and your daily use experience.

Because RAM memory is larger and cheaper than in the past, many users today have enough memory and almost no longer need to use swap files. The obvious benefit is that the swap process does not take up any system resources, and the cached files are not moved back and forth from RAM to swap and vise Versa for no reason.

How to change Swappiness?

The swappiness parameter value is stored in a simple configuration text file in / proc / sys / vm, named “swappiness”. If you browse the file in the file manager, you can find the file and open it to check the exchangeability of your system. You can also check or change it on the terminal by entering the following command (faster):

sudo sysctl vm.swappiness=10

Or some other value between “0” and “100” instead of the value “10” I used. To ensure that the swap value is changed to the desired value, simply type:

cat /proc/sys/vm/swappiness

Output a valid value to the terminal again.

This change has an immediate effect on the operation of the system and does not require a reboot. In fact, restarting will restore the swap to the default value (60). If you have thoroughly tested the required swap values ​​and found that the swap values ​​work reliably, you can make the changes permanent by navigating to /etc/sysctl.conf (another text configuration file). You can open it as superuser (administrator) and add the following line at the bottom to determine swapability: vm.swappiness = “here is your expectation”. Then, save the text file and you are done!

Change the swappiness value of the Linux system


When changing settings, consider some mathematical operations related to swapping. Setting the parameter value to “60” means your kernel will swap when the RAM reaches 40% capacity. Setting it to “100” means your kernel will try to swap everything. Setting it to 10 (like I did in this tutorial) means that swap will be used when 90% of RAM is used, so if you have enough RAM memory, this may be a safe option and can be easily To improve system performance.

Although some users want the full cake, this means that they set the swap to “1” or even “0”. “1” is the smallest “active swap” setting, while “0” means that swap is completely disabled and only restored when the RAM is completely full. Although these settings are still theoretically usable, testing them on a low-spec system with 2GB of RAM or less may cause freezing and make the operating system completely unresponsive. In general, to find the golden meaning between overall system performance and response latency, some experimentation is required (as always).