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.
1 2 3 4 5 |
# cat /proc/PID/smaps | grep -i swap Swap: 10 kB Swap: 5 kB Swap: 500 kB ... |
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.
1 2 |
# cat /proc/PID/status | grep VmSwap VmSwap: 515 kB |
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.
1 2 3 4 5 6 7 8 9 10 |
# for file in /proc/*/status ; do awk '/VmSwap/||/^Pid/||/Name/{printf "%-20s", $2}END{ print ""}' $file; done ... Xorg 2297 0 crypto 23 atd 2306 0 cron 2370 0 sshd 2479 0 gdm-session-wor 2518 0 squid 13455 100 ... |
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
.
1 |
$ sudo apt-get install smem |
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.
1 2 3 4 5 |
# cd /tmp # wget http://www.selenic.com/smem/download/smem-1.4.tar.gz # tar xvf smem-1.4.tar.gz # cp /tmp/smem-1.4/smem /usr/local/bin/ # chmod +x /usr/local/bin/smem |
To view swap usage per process, run smem
without any arguments.
1 2 3 4 5 |
# smem PID User Command Swap USS PSS RSS ... 13455 squid squid 100 27536 30986 53328 ... |
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.