Loading...
 

Linux Swap

Swap space usage becomes an issue usually only when there is not enough RAM available, and the kernel is forced to continuously move memory pages to swap and back to RAM, just to keep applications running. In this case, vmstat will show if anything the system is swapping.

If the system is swapping when it shouldn’t (REMEMBER sometimes it may be more efficient to keep the I/O cache in memory and still swap) changing the kernel swappiness value could help. This is going to require some tweaking because, as mentioned, depending on the application usage, some swapping may be more desirable than using space reserved for I/O cache.

The default vm.swappiness value is 60, setting it to 0 means “never use swap when there is still RAM left“ and 100 is swapping out memory as soon as possible.

To change the value temporarily (lost on reboot):

$ sudo sysctl vm.swappiness=10

To change the value permanently: Edit the file /etc/sysctl.conf

What is using my swap?

$ for DIR in `ls -d /proc/[0-9]*` ; do grep Swap $DIR/smaps 2>/dev/null | awk -v p=$DIR 'BEGIN{s=0;}{s=s+$2;}END{if (s!="0") print "Process: "p" - Swap: " s}';  done|sort -grk5
Process: /proc/3573 - Swap: 193104
Process: /proc/4299 - Swap: 13624
Process: /proc/4204 - Swap: 2412
Process: /proc/14768 - Swap: 1904
Process: /proc/16687 - Swap: 1900
Process: /proc/16684 - Swap: 1900
Process: /proc/16682 - Swap: 1900
Process: /proc/16688 - Swap: 1888
Process: /proc/16685 - Swap: 1888
Process: /proc/16683 - Swap: 1888
Process: /proc/16689 - Swap: 1872
Process: /proc/16686 - Swap: 1872
Process: /proc/22520 - Swap: 1256
Process: /proc/23615 - Swap: 1152
Process: /proc/4312 - Swap: 508
Process: /proc/4138 - Swap: 476
Process: /proc/1258 - Swap: 448
Process: /proc/4169 - Swap: 212
Process: /proc/3504 - Swap: 208
Process: /proc/4205 - Swap: 196
Process: /proc/4111 - Swap: 152
Process: /proc/4026 - Swap: 128
Process: /proc/4213 - Swap: 124
Process: /proc/5813 - Swap: 88
Process: /proc/4382 - Swap: 80
Process: /proc/4381 - Swap: 80
Process: /proc/3995 - Swap: 80
Process: /proc/4380 - Swap: 76
Process: /proc/4379 - Swap: 76
Process: /proc/4378 - Swap: 76
Process: /proc/4377 - Swap: 76
Process: /proc/3631 - Swap: 68
Process: /proc/3992 - Swap: 64
Process: /proc/5814 - Swap: 60
Process: /proc/1 - Swap: 60
Process: /proc/5816 - Swap: 40
Process: /proc/5815 - Swap: 40
Process: /proc/5818 - Swap: 28
Process: /proc/5817 - Swap: 28
Process: /proc/4327 - Swap: 24