If you execute the free -m
command you may notice that the operating system can use some swap even if there is still RAM free.
This behaviour is controlled by the Linux kernel parameter vm.swappiness
and can be changed by setting its value from 0 to 100.
The swappiness in Linux is a rate in which the operating system tends to write data out of the RAM onto the disk drive (HDD or SSD).
In this note i’ll show how to check and how to change the swappiness in Linux.
Cool Tip: How to disable swap in Linux! Read more →
Swappiness in Linux
Swappiness | Description |
---|---|
0 |
Swap usage will be avoided unless it is absolutely necessary (out of memory). |
60 |
The default value of vm.swappiness in Ubuntu/CentOS. |
100 |
Aggressive swapping. Programs will be swapped to disk almost instantly. |
Check the current swappiness value, by typing:
$ cat /proc/sys/vm/swappiness
To change the current swappiness value, execute:
$ sudo sysctl vm.swappiness=<VALUE> - example - $ sudo sysctl vm.swappiness=60
To set the swappiness value persistently, so it stays after reboot, create the /etc/sysctl.d/99-swappiness.conf
file and define there the vm.swappiness=<VALUE>
, for example:
$ sudo echo "vm.swappiness=60" | sudo tee -a /etc/sysctl.d/99-swappiness.conf
To turn off swap (move swap contents back to the RAM) and then re-enabled it, run:
$ sudo swapoff -a $ sudo swapon -a
That one liner to modify the config file for the swappiness is a life saver. I lazily followed another tutorial and messed up by conf file with VIM ended up with a blank swappiness conf file and after reboot expected one of two things to happen for the system to crash or the file to magically reappear. None of the two happened guess it’s only a override and the true default value is stored somewhere safe from numbskulls
Many thanks for your information!