Tuesday, January 12, 2016

What is Swappiness (vm.swappiness) and how to change it on your Linux Server

What is Swappiness?

In Linux, the "swap area" is a dedicated space in your hard drive that is usually set to at least twice the capacity of your RAM, and along with it constitutes the total virtual memory of your system. From time to time, the Linux kernel utilizes this swap space by copying chunks from your RAM to the swap, allowing active processes that require more memory than it is physically available to run.

Swappiness is the kernel parameter that defines how much (and how often) your Linux kernel will copy RAM contents to swap. This parameter's default value is “60” and it can take anything from “0” to “100”. The higher the value of the swappiness parameter, the more aggressively your kernel will swap.

Why change it?

The default value is an one-fit-all solution that can't possibly be equally efficient in all of the individual use cases, hardware specifications and user needs. Moreover, the swappiness of a system is a primary factor that determines the overall functionality and speed performance of an OS. That said, it is very important to understand how swappiness works and how the various configurations of this element could improve the operation of your system and thus your everyday usage experience.

As RAM memory is so much larger and cheaper than it used to be in the past, there are many users nowadays that have enough memory to almost never need to use the swap file. The obvious benefit that derives from this is that no system resources are ever occupied by the swapping process and that cached files are not moved back and forth from the RAM to the swap and vise versa for no reason.

Factors for consideration

There are some maths involved in the swappiness that should be considered when changing your settings. The parameter value set to “60” means that your kernel will swap when RAM reaches 40% capacity. Setting it to “100” means that your kernel will try to swap everything. Setting it to 10 (like I did on this tutorial) means that swap will be used when RAM is 90% full, so if you have enough RAM memory, this could be a safe option that would easily improve the performance of your system.

Some users though want the full cake and that means that they set swapping to “1” or even “0”. “1” is the minimum possible “active swapping” setting while “0” means disable swapping completely and only revert to when RAM is completely filled. While these settings can still theoretically work, testing it in low-spec systems of 2GB RAM or less may cause freezes and make the OS completely unresponsive. Generally, finding out what the golden means  between overall system performance and response latency requires quite some experimentation (as always).

Here some help to understand the values:
vm.swappiness = 0 -> The kernel will swap only to avoid an out of memory condition. See the "VM Sysctl documentation".
vm.swappiness = 1 -> Kernel version 3.5 and over, as well as kernel version 2.6.32-303 and over: Minimum amount of swapping without disabling it entirely.
vm.swappiness = 10 -> This value is sometimes recommended to improve performance when sufficient memory exists in a system.
vm.swappiness = 60 -> Default value.
vm.swappiness = 100 -> The kernel will swap aggressively.

How to change it?

The swappiness parameter value is stored in a simple configuration text file: /proc/sys/vm/swappiness. To check the value that it's setted on your system, you can do:
user@ximunix:~$ sudo cat /proc/sys/vm/swappiness

You can change the value by editing the file or by running:
user@ximunix:~$ echo 10 > /proc/sys/vm/swappiness
user@ximunix:~$ sudo sysctl vm.swappiness=10

Or whatever value between 0 and 100 that fits your system and needs.

To ensure that the swappiness value was correctly changed to the desired one, you simply type:
user@ximunix:~$ sudo cat /proc/sys/vm/swappiness

This change has an immediate effect in your system's operation and thus no rebooting is required. In fact, rebooting will revert the swappiness back to its default value (60). If you have thoroughly tested your desired swapping value and you found that it works reliably, you can make the change permanent by navigating to /etc/sysctl.conf. You may open this as root and add the following line on the bottom to determine the swappiness:

Then, save the text file and you're done!