Linux swap space - real usage

Wednesday, January 19, 2011

Using top, ps aux, etc etc gives all kinds of memory usage with lots of blog posts and forum questions explaining how difficult they are to understand, and how typically aren't actually that useful.

As of kernel 2.6.14, smaps has been introduced which means each process reports its own memory usage breakdown.

smaps detail for a process can be seen by:

sudo cat /proc/PID/smaps
where PID is the process ID of the process you're interested in. Note you won't see any content if not the process owner or root.

So, use top, find the big memory hoggers first. Then look at the smaps file for the swap usage. You can use, for example:
sudo grep Swap /proc/30888/smaps | cut -d" " -f 10-20 | grep -v ' 0 kB'
where 30888 is an eg PID.

For more advanced smaps analysis, try the smaps.pl tool. Not sure at this stage, though, whether that tool gives separate swap values.

In Debian at time of this post, the pmap tool has been patched to use smaps, which may then offer swap values in its output.