A little about SWAP

Posted by on October 31, 2011 in Operating Systems, Ubuntu | 0 comments

A little about SWAP

Swap has been a major pain in my tush while managing Ubuntu Systems. Even my one machine with 16 gigs of RAM still finds itself in swap after a while. So I’ve been hunting around trying to come up with solutions to swapping machines, and how to keep machines out of swap.

What is Swap

From my experience, swap is NEVER a good thing, and your machine will never leave swap once its in that state, even if the memory demand decreases to the point where everything could be put back into working memory. Swap is the physical place on a hard disk where working memory (RAM) can move information to hold until it has enough space to use it again. The issue here is that access of the hard drive is soooooo much slower than access of RAM and your system could wind up spending all its time moving information back and forth from the harddrive to working memory and never actually do anything with the information in working memory. This essentially kills your machine and you need to reboot in order to clear everything out.

There are two good things I could find that swap can do, but neither should be applicable in the server world. The first is that big third party software may use a ton of memory and swap may be used to handle the extra load. The second is hibernation of a machine. Swap is used there to save the working memory to disk so that the working memory can be cleared and shut off during hibernation. Neither should be used on a server however.

Monitor Your Swap

There’s a few ways to monitor swap usage.
To get a summary of the swap.


To get a more detailed listing of swap usage.

#Press O (the letter O) then P to sort by swap usage.

To get a really detailed view. Note that I did not make this script. The author at http://northernmost.org/blog/find-out-what-is-using-your-swap/ made it. Create an executable file and run it as root.

# Get current swap usage for all running processes
# Erik Ljungstrom 27/05/2011
for DIR in `find /proc/ -maxdepth 1 -type d | egrep “^/proc/[0-9]”` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm –no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk ‘{ print $2 }’`
echo “PID=$PID – Swap used: $SUM – ($PROGNAME )”

echo “Overall swap used: $OVERALL”

This second command is the more useful in my opinion as it will tell you specifically just the processes that are using swap, which is what you’d be interested in for this case.

./getSwap.sh | egrep -v “Swap used: 0” |sort -n -k 5

Some things to do about it

This setting has worked very well for me on a number of systems. Swappiness is the tendency of the server to use swap instead of squeezing the information into working memory. The values are between 0 and 100, with 0 telling it to try to avoid swap at all costs, and 100 being very liberal in the swap usage.

The default value in Ubuntu for swappiness is 60, but for me this way far too high for a server. I don’t ever want it to use swap, so I set my value to 0. After I did this, servers that would routinely go into swap run without any swap usage at all. Swappiness can be changed in the following file.

#edit /etc/sysctl.conf

Even with swappiness set low, the machine may still go into swap because of a huge load suddenly bombarding it, or for some other reason. Therefore you can manually empty the swap.

Empty the Swap
This script is written and suggested by Ubuntu.com, I did not write this script. Create an executable file with the following contents:

err=”not enough RAM to write swap back, nothing done”
mem=`free|grep Mem:|awk ‘{print $4}’`
swap=`free|grep Swap:|awk ‘{print $3}’`
test $mem -lt $swap && echo -e $err && exit 1
swapoff -a && swapon -a &&
exit 0

I have a monitoring script that alerts me whenever swap usage occurs. I would then log in and run this script if the usage isn’t too bad. Obviously if the machine is unresponsive, the chances of being able to put all the swap back into working memory are slim, so a restart may be required. It would be possible to create a script to monitor the swap, and run this script if any swap is present if you do not want to log into the system, or if you just want it taken care of. I would only suggest this if you don’t want to find the cause of the problem and only remedy the result of the problem.



If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment

Your email address will not be published. Required fields are marked *