The Linux Page

Looking at how much swap memory each process uses?

RAM & Swap memory exchange, but who uses it?

Limited Information...

When a computer swap is getting used, by default you just see that swap is used, not so much about which process uses that swap space.

The function I used by default is free which outputs that info:

$ free
          total     used      free  shared buff/cache available
Mem:  528011648 75782220 200245172 1928996  251984256 449183156
Swap:  30769148        0  30769148

As we can see, this tool shows use how much RAM is used/available and how much swap is in use/available.

Only that doesn't tell you much. That is, my main question is genrally: which process uses the most swap space?


There is a small command line tool written in Python called smem which does that for you. It reads the information from each process data file under /proc/<process ID>/....

The cool feature here is that you can sort the resulting table using the swap column, that way you see the hogs at the bottom.

smem -k -s swap

Note that if you use it without sudo the list of processes will be limited to the processes you own.

In many cases, you want to see how much swap is used by daemons so that means you need to use sudo like so:

sudo smem -k -s swap

And as a result, you may see how much memory mysqld is using:

 PID User     Command              Swap      USS      PSS      RSS
1628 mysql    /usr/sbin/mysqld   100.7M    40.0M    40.1M    42.6M

As we can see in my instance, the swap used by MySQL is 100.7Mb. That's quite a bit on a small machine (2Gb of RAM total on that VPS) and that's not the only process running on that machine.

Another interesting fact about that, it allows you to search for all the processes that hog memory and look into reasons for the leaks. For servers, it's often difficult to tell whether they leak or not as it can take a long time to see the memory leakage if it is very small... but after running the process for months at a time, it can end up being a very large amount... (note that I don't think that MySQL leaks, I'm talking about my servers which I think for some do leak a little here and there over time—they may be hording various small objects which should be deleted along the way but are not).

My main problem with this one is... installing it requires at least 50 other python packages. I don't have too much against python, but on a machine where I want to save as much disk space as I can, it's not that great to have to install another 50 pythong packages... (even if most are fairly small).


I wrote my own script which essentially does the same thing and uses a total of 2 or 3 blocks on your hard drive (592 bytes in the current version).

The script is a basic shell script. It reads the /proc/<id>/... info and capture the VmSwap fields.

The final is sorted by swap size.

Here is the script as it stands in Sep 2020:

# Retrieve each process VmSwap size and display it with its name if not zero.
# This gives you a list of processes using swap memory in increasing order.

sizes() {
  for f in /proc/[0-9]*/status
    rm -f /tmp/swap-usage
    if cp $f /tmp/swap-usage
      size=`grep 'VmSwap:\s*' $f | grep -v ' 0 kB' | sed -e 's/VmSwap:\s*//'`
      if test -n "$size"
        name=`grep Name: $f | sed -e 's/Name:\s*//'`
        echo "${name}: $size"

# To reverse the sort, use 2nr instead of just 2n
sizes | sort -k 2n

# vim: ts=2 sw=2 et

The source is available in the Snap! Websites project.