Tuesday, April 22, 2014

Linux - Swapping

The Linux Kernel provides a tweakable setting that controls how often the swap file is used, this parameter is called swappiness. This parameter is located at /proc/sys/vm/ directory. The default setting of swappiness is 60, meaning that the swap file will be used fairly often if the memory usage is around half of the RAM. You can check your own system's swappiness value by running:
# cat /proc/sys/vm/swappiness

A swappiness setting to 0 means that the disk will be avoided unless absolutely necessary (when you are running out of memory), so even if you have swappiness set to 0, you could still see some swapping happening. A swappiness setting of 10 means that programs will be swapped to disk almost instantly.

To change the value of swappiness:
# echo 0 > /proc/sys/vm/swappiness

To change it permanently:
# vi /etc/sysctl.conf

add the following
vm.swappiness=0

Which process is swapping?
To find out which process is swapping, run the "top" command then press "O" followed by "p" then "Enter". Processes will be sorted by swap usage.

One thing you should know that it is not possible to get the exact size of used swap space of a process. Top fakes this information by making SWAP = VIRT - RES, but this is not a good metric, because other things such as video memory counts on VIRT as well.

Here is a script to check what is consuming your swap:
check-swap.sh

Clear swap:
You can also clear your swap by running "swapoff -a" and then "swapon -a" as root instead of rebooting to achieve the same effect.

No comments: