Linux: Swap Usage per Process

You run the free command on a Linux machine and it reports that swap space is being used. How do you determine if a process is using swap memory? Or how do you determine which processes are using swap memory? The short answer is to use the procfs or /proc filesystem.

From Linux 2.6.14, the smaps file shows memory consumption for each of the process’s mappings. Each mapping has information on swap usage.

You may get a few entries for swap usage and you will need to sum them all up to get the swap usage for that process. A small shell script is probably needed to do that.

There is an easier way – to look at the status file instead.

List All Processes Using Swap Space

If you are interested into finding out all the processes that are using space space, use the for loop command.

The command will print out the process name, process id and the swap memory usage in kB. If you notice, for the crypto process, the swap usage is not there, that means the VmSwap field was not found for that process. You can also see that the squid process is using 100 kB or swap space.

smem – Memory Reporting Tool

If you would prefer a standard utility to check on swap usage, install smem. On Debian or Ubuntu, install it using apt-get.

On other Linux distributions like Fedora, RHEL, CentOS, SLES, etc, you can download the compiled binary from the official website. Use the following commands to copy the smem command into the /usr/local/bin directory.

To view swap usage per process, run smem without any arguments.

smem takes in a couple of arguments and can give us various memory usage reports – by user, by system, in percentage, etc. For more details, you can refer to the smem man page.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }