How to prevent the use of the memory swap area before the RAM runs out.

How to prevent the use of the memory swap area before the RAM runs out

Miguel Menéndez

You may have come across a machine that gets bogged down because dumping data from RAM to disk (partition or file swap) is consuming almost all of your processor time.

If you use top (if the system is not completely soaked), you will see that the kswapd0 process is consuming close to 100% of the processor time and, although the RAM is at 40%, it is using the partition o the memory swap file (swap).

$ top
top - 10:05:40 up 24 min,  3 users, load average: 12,17, 8,62, 5,53
Tasks: 256 total,   8 running, 248 sleeping,  0 stopped,   0 zombie
%Cpu(s): 46,4 us, 29,4 sy,  0,0 ni, 10,9 id, 11,2 wa, 1,3 hi,  0,8 si, 0,0 st
MiB Mem :   7820,1 total,    101,1 free,   3645,8 used,  4073,2 buff/cache
MiB Swap:      0,0 total,      0,0 free,      0,0 used,  3049,6 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    105 root      20   0       0      0      0 R  99,9  0,0   0:15.57 kswapd0
[...]

This is because the default value of the swappiness parameter is set to 60 (0 - 100). To prevent the use of swap until RAM is exhausted, the value of swappiness should be 1 (use swap when RAM has 1% free). If what matters is that swap is not used at any time, even if the RAM is completely exhausted, the value of swappiness should be 0.

Check the value assigned to the swappiness parameter:

$ cat /proc/sys/vm/swappiness

Most likely, as I said before, it will return 60.

To change that value temporarily, until the machine is rebooted:

$ sudo -i
# echo 1 >/proc/sys/vm/swappiness

If we want the change to be permanent:

$ sudo sysctl vm.swappiness=0

Source: Wikipedia .

Header photo by rleathers .

Comments

Found a bug? Do you think something could be improved? Feel free to let me know and I will be happy to take a look.