Skip to content

A Quick Look at Swap Use in Linux

There can be times when you would like to quickly check how your Linux system is handling swap.

Tux the penguin arranging blocks of memory

Until recently, it was not something I looked at regularly until, as it turned out, a commercial UPS monitoring program began consuming an enormous amount of swap. My analysis of that issue took me on a brief journey ending in a short shell script based on what I found elsewhere. I want to share that script here.

Since then, further checks of swap have helped identify and resolve a couple of other minor issues which may have gone uncorrected otherwise, so now swap checks are part of my routine.

As for the commercial UPS monitoring program – a full reinstall with an update did not help, so its punishment is to be restarted weekly by a cronjob.

The Usual Method

The standard one-liner for checking swap usage produces a very useful (though raw) look at some of the most swap-consuming processes.

# for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | more

This and similar variations have been in use for some time now.

Credit to The Geek Diary1 for the version above.

If you run that command, this is the sort of thing you will see;

Example Output

qemu-system-x86 307444 kB
miniserv.pl 20196 kB
networkd-dispat 7956 kB
(sd-pam) 2820 kB
(sd-pam) 2816 kB
cleanupd 2256 kB
lpqd 2204 kB
udisksd 1760 kB
gvfs-udisks2-vo 1336 kB
smartd 1316 kB
dhclient 1060 kB
systemd-udevd 1056 kB
virtlogd 1040 kB
at-spi-bus-laun 924 kB
systemd 916 kB
...

The line above is generally all you would need for a brief check of swap, but on this occasion, it seemed that a properly scripted version would probably be more convenient and repeatable in the long term.

A Slightly Neater Way

For my systems, the following script is now saved as ~/bin/swaptop

#!/bin/sh
set -e
numlines="${1:-15}"
  # Gather the process names and swap usage from the status information in 
  #  /proc, sort it, trim to specified no. of lines and prepare it for display.
  for procs in /proc/*/status
  do
    # /proc is volatile. Check each "status" exists immediately before awk.
    if [ -f "${procs}" ]; then
      awk '/Name|VmSwap/{printf $2 " " $3}END{ print ""}' "$procs"
    fi
  done \
   | sort -k 2 -n -r | head -n "${numlines}" \
   | awk '{printf "\033[93m%7d\033[0m %3s %-20s\n", int(0.5+($2/1024)),"MiB",$1}' 
exit 0

(Don’t forget chmod +x swaptop)

Firing this off with swaptop defaults to the top 15 swap-consuming processes in MiB (rather than kiB) in a neat table.

Example Output

    304 MiB qemu-system-x86
     20 MiB miniserv.pl
      8 MiB networkd-dispat
      3 MiB (sd-pam)
      3 MiB (sd-pam)
      2 MiB cleanupd
      2 MiB lpqd
      2 MiB udisksd
      1 MiB gvfs-udisks2-vo
      1 MiB smartd
      1 MiB dhclient
      1 MiB systemd-udevd
      1 MiB virtlogd
      1 MiB at-spi-bus-laun
      1 MiB systemd

The script will also take in an integer as the first option for the number of lines, so the following will give you the top 30 processes;

swaptop 30

I hope this is helpful. All the best.