The Swappiness value is a part of the Linux Kernel. If you are a Linux user, you should have heard of Linux Swap space. During the Linux installation, a small amount of hard disk space is allocated as a Swap. This Swap partition or file acts like RAM when the system memory is exhausted. And the Swappiness Value determines when to use the swap space.
In this post, I am going over some of the basics of Linux Swap and the Swappiness Value. And also how you can change the Swappiness Value on Linux.
What Is Swap?
Linux Kernel divides its physical memory (a.k.a RAM) into chunks of memory called pages. When applications need more memory, these pages can be allocated to them. If your system is running out of physical memory, the Linux kernel starts swapping inactive pages to the swap partition. Swap is a preallocated hard disk space that is used to increase the virtual memory of your system.
Swap is a breather to your system when the RAM is exhausted. If you do not have a swap partition or file and you are running out of memory, the Linux OOM kicks in and starts killing processes to free up memory. Adding additional swap space will help you to avoid Out of Memory conditions.
But the biggest drawback of swap is the low performance. As HDD is 100,000 times slower and SSD is 1000 times slower than RAM, it is not possible to overcome the memory shortage problems with it. If you constantly run out of memory, you need to install additional RAM. Swap only helps your system be somewhat responsive. So that you can avoid out-of-memory conditions.
What Is Swappiness?
Swappiness is a Linux kernel’s preference that is used to swap out processes from RAM to the swap partition. It is a balance between RAM and Swap space. When your RAM reaches the swappiness value, the Linux kernel starts swapping out idle processes to the swap partition to free up the RAM for active processes. This will degrade performance but prevent your system from crashing.
Swappiness can have a value between 0 to 100. A value of 0 will disable swappiness and 1 is the minimum value. A value of 100 will swap out processes too aggressively and degrade the performance. The recommended value is 10 if you want to use most of your RAM and swap when necessary. But it will always depend on your workload.
What Does Swappiness Do?
Swappiness is a parameter for the Linux kernel. This parameter tells the Linux kernel when to swap out memory pages to the swap partition. Aggressive swappiness will massively degrade performance. If you want system responsiveness, you need to maximize the RAM usage and avoid swapping.
A swap partition is slow and you should not use this most frequently. Lower the value according to your needs. If you are on an old computer with very little RAM (i.e. less than 4GB), you may need to increase the Swap Size and the swappiness value. If you have 32GB or more RAM, lower the swappiness value as much as possible to increase system responsiveness.
Why Should You Change The Swappiness Value?
Many Linux users do not care about the swappiness value. Some do not know what it is and some just don’t care about it. But there are some good reasons to change the swappiness value.
By default, the swappiness value is 60. And this value can work with most systems. But if you have a large amount of RAM (i.e. 16GB or higher), you should change the swappiness value. A higher swappiness value will swap out memory pages and degrade performance. If you have a large amount of RAM then you should lower the value below 60. Many Linux users including me suggest the value of 10.
Swappiness set to 10 will let your system use the RAM as much as possible. As RAM is much faster than a hard drive, the overall system performance remains snappy. But if your system has less than 1GB of RAM, then you should set the swappiness value a bit higher. This will prevent system crashes and improve performance.
How To Check For The Swappiness Value?
By default, major Linux distributions use the value of swappiness to 60. You can see the current swappiness value by using the following sysctl command.
sudo sysctl vm.swappiness
Output: vm.swappiness = 60
Another command you can use to see the swappiness value is the following cat command.
How To Change The Swappiness Value?
Swappiness can have a value between 0 to 100. A value of 0 turns off the swappiness and a value from 1 to 100 instructs the kernel to swap out processes. A higher value will aggressively be swapping processes out of physical memory. A lower value will make the kernel avoid swapping as long as possible.
As accessing swap memory is considerably slower than the physical RAM, a lower swappiness value will most likely improve overall system performance. The default value of 60 is appropriate if you have less than 4GB of RAM. If you have more than 4GB of RAM, for a regular desktop installation, a value of 10 is recommended. For database servers, a value of 1 is recommended.
These are all recommendations. The optimal swappiness value depends on your system workload and the physical memory (RAM) of your system. You can use the following command to set a swappiness value and adjust the parameter on a small scale to find an optimal value.
sudo sysctl vm.swappiness=[VALUE_0_TO_100] e.g. sudo sysctl vm.swappiness=10
The above command will allow you to change the swappiness value at runtime. Use different values to find out the optimal performance. This command will not make the swappiness value permanent. Every time you reboot, the value will be changed back to the default. To make it permanent, open the file /etc/sysctl.conf with a text editor and change the value of the following entry vm.swappiness=[YOUR_VALUE]. (Add the line if it does not exist in the file).
sudo nano /etc/sysctl.conf
You can use any text editor you want. If you would like to use gedit or xed, replace the nano with gedit or xed and update the /etc/sysctl.conf file to change the swappiness value permanently.
I am using nano here. Open the terminal and enter the above command.
Find the vm.swappiness parameter and change the value. If it is not available, write the parameter like this vm.swappiness=10. I am using 10 here as the swappiness value. You can use your value that suits your needs.
Save the file by pressing ctrl+o and Enter. And press ctrl+x to exit.
So we have learned what swappiness is, how it works, and how to change the Swappiness Value to increase performance. I hope you now have a clear knowledge about the swap and swappiness value.
If you like this tutorial, don’t forget to share it with your friends. And if you have any questions, you can ask me in the comment section below. I would love to answer your questions.