Tune the way linux use the swap with the swappiness parameter


On linux, you can tune the way the system use the swap via the 'swappiness' parameter.


Check how the swap is used on your machine:

You can print how the system is using the swap with the vmstat command :

thomas@home:~$ vmstat 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0  25192  81956  93052 2383988    0    0     8    15    1    1  1  1 98  0
 6  0  25192  71076  93100 2384224    0    0    10   272 1148 1265  8 10 80  2
 0  0  25192  97492  93016 2368148    0    0   132  1186 1380 1845 26 35 39  0
 0  0  25192  96648  93032 2368740    0    0    70   188 1121 1066  2  2 96  1
 0  0  25192  96400  93044 2368976    0    0     0   188 1117 1082  1  1 98  0

the '2' parameter stands for the refresh time in seconds.

si stands for Swap IN (byte written to the swap)
so stands for Swap OUT (byte removed from the swap)

On a server which has enough RAM, I observed that there was regularly swap IN activity.
So that the server use more the RAM and less the swap, we need to tune the swappiness parameter.


By default, on ubuntu, it is set to '60'.
Basically, 0 means do not use the swapp, 100 means use it a lot.

You can check your current system value with this command :

cat /proc/sys/vm/swappiness

You can set it on the fly with the following command :

echo 40 > /proc/sys/vm/swappiness

run vmstat again and see if it changes anything on the swap activity.
On my server, with this parameter, the swap IN activity stopped.

But with this way of setting the swappiness, the parameter value you set won't last after a reboot, it will be reset to default value.

To change definitively the swappiness value, you need to edit the /etc/sysctl.conf
and add the following line at the end of the file :

#swapping tendancy 0 :no swap, 100 : full swapping
vm.swappiness = 40

save your change and then run sysctl like this :

sudo sysctl -p

it should print
vm.swappiness = 40

reboot and recheck the
cat /proc/sys/vm/swappiness

the parameter should have keep the new value.

Recheck vmstat output to see how the situation has evolved.

picture from http://www.icon-king.com/

Comments

Popular posts from this blog

Upgrade Slimframework v3 to v4, how I did it

Reset Bacula database and files

Limit the upload bandwidth of your apache webserver with mod_bw