1. Overview
In this tutorial, we’ll discuss how we can check for processes that use swap memory. First, we’ll cover the /proc directory to see what it contains and how we can extract processes’ information from it. Afterward, we’ll write a shell script that will automatically extract the swap usage information used by the processes.
Last, we’ll use the smem tool as an alternative to check for processes that use the swap memory.
2. The /proc Directory
On Linux, proc is a special directory that contains information about the Linux kernel, its configuration, and processes loaded into the physical memory. The operating system creates this directory once we boot into the Linux machine. Moreover, the virtual files inside this directory have no physical size on the disk. The information inside these files is generated on the fly once we read them.
Now that we have an idea about the proc directory, let’s see what’s inside it:
$ ls -l /proc
total 0
dr-xr-xr-x 9 root root 0 Feb 17 15:35 1
dr-xr-xr-x 9 root root 0 Feb 17 15:35 10
dr-xr-xr-x 9 root root 0 Feb 17 15:35 100
...
dr-xr-xr-x 9 hey hey 0 Feb 17 22:12 34160
...
As we review the list of items inside this directory, we’ll notice that there are a lot of numbered directories, named directories, and other virtual files. However, we’re only concerned with the numbered directories because they correspond to each running process. The number of the directory is actually the PID of the process.
As we can see in the above snippet, it also lists the user processes. So, let’s list the items inside the “34160” directory to see what it contains:
$ ls -l /proc/34160
total 0
...
-r--r--r-- 1 hey hey 0 Feb 17 22:12 stat
-r--r--r-- 1 hey hey 0 Feb 17 22:12 statm
-r--r--r-- 1 hey hey 0 Feb 17 22:12 status
...
The virtual files inside the directory will contain a plethora of information regarding the process. But in our case, we’re interested in the status virtual file because it contains the memory usage information.
Since we’re only interested in the swap usage, we’ll grep the VmSwap field from the file:
$ cat /proc/34160/status | grep VmSwap
VmSwap: 0 kB
Now that we know how to check for swap memory used by a process, we can write a shell script to print a list of processes using the swap memory.
2.1. Shell Script to List Processes using Swap Memory
The shell script will go through each numbered directory in the proc directory and print the swap usage it extracts from the status file. It’ll also print the name and PID of the process as well:
#!/bin/bash
overall=0
for status_file in /proc/[0-9]*/status; do
swap_mem=$(grep VmSwap "$status_file" | awk '{ print $2 }')
if [ "$swap_mem" ] && [ "$swap_mem" -gt 0 ]; then
pid=$(grep Tgid "$status_file" | awk '{ print $2 }')
name=$(grep Name "$status_file" | awk '{ print $2 }')
printf "%s\t%s\t%s KB\n" "$pid" "$name" "$swap_mem"
fi
overall=$((overall+swap_mem))
done
printf "Total Swapped Memory: %14u KB\n" $overall
The script is pretty self-explanatory. Let’s run it in the terminal:
$ ./swp.sh
1658 Discord 41448 KB
1681 Xwayland 15772 KB
18621 firefox 213204 KB
18872 WebExtensions 60912 KB
...
Total Swapped Memory: 1177212 KB
We can easily customize the output further by using something like the column tool.
3. Using the smem Utility
The smem tool displays memory usage of processes. Apart from showing the RSS, PSS, and USS memory, it can also show the swap memory.
By default, it doesn’t ship with most Linux distributions. So, we’ll have to install it from our distribution’s official repository.
3.1. Installation
The smem utility will be available under the smem package name. We can use a package manager like yum or apt to install it.
For Debian and its derivatives:
# apt install smem
For Fedora, OpenSUSE, and RHEL:
# yum install smem
3.2. Usage
Once smem is installed, we can try it out in the terminal:
$ smem
PID User Command Swap USS PSS RSS
494 hey swaybg -o DVI-I-1 -i /home/ 15136 4 86 2304
1665 hey /opt/discord/Discord --type 8800 0 96 820
589 hey /usr/lib/pulse/gsettings-he 1224 20 122 2372
37348 hey /bin/sh /usr/bin/android-st 204 164 180 1220
...
Let’s say we want to print the top 10 processes sorted by swap memory usage in descending order. We can do so with the -s or –sort option:
$ smem -s swap -r | head -n10
PID User Command Swap USS PSS RSS
18943 hey /usr/lib/firefox/firefox -c 385536 161836 165339 213496
18621 hey /usr/lib/firefox/firefox 225660 458788 479474 576556
1748 hey /opt/discord/Discord --type 84652 130220 137640 153064
1658 hey /opt/discord/Discord 76108 28336 34916 51324
18872 hey /usr/lib/firefox/firefox -c 65172 133824 135810 169480
33599 hey /usr/lib/firefox/firefox -c 55012 261264 266269 324880
20578 hey megasync 52252 13272 15308 24040
35605 hey /usr/lib/firefox/firefox -c 46584 504264 507708 554040
1705 hey /opt/discord/Discord --type 41236 13876 18735 29988
The -r option will reverse the output, which will print the list in descending order.
4. Conclusion
In this brief tutorial, we saw how we could view the swap memory used by processes in Linux. We accomplished that by viewing the processes’ information inside the proc directory and using the smem tool.