Introduction

Swap space or swap memory is a supplementary memory available for the operating systems’ use when the physical memory has been fully utilized or is close to exhaustion. When the amount of available physical memory (RAM) is running very low, at that juncture the operating system swaps out (moves) some memory pages that have not been accessed for a while to the swap memory so as to free up physical memory. The swapped out memory pages are moved back to physical memory when sufficient amount of resources are available. Linux provides multiple utilities to measure the overall system swap utilization. Some of these utilities are top, vmstat and the free command.
But these utilities do not display the amount of swap memory utilized by a single process. In this article, we will demonstrate three different techniques using which we can obtain the amount of swap memory used by an individual process.

Method 1: Using VmSwap parameter in /proc file for the process
The /proc file system in Linux stores the current status information about a running process. Most of the information contained in the /proc file system is in the form of text files and can be displayed using cat command. The VmSwap parameter present in the status file for a given process represents the amount of swap memory used by it. Let’s check the swap used by the sshd daemon. To get the PIDs associated with the sshd daemon we could use the pgrep command as shown below.

[root@linuxnix ~]# pgrep sshd
27657
38984

To view the VmSwap value for this process we’ll access the status file associated with it.

[root@linuxnix ~]# grep -i VmSwap /proc/27657/status
VmSwap: 0 kB
[root@linuxnix ~]#

In case we have multiple PIDs assoicated with a process like we had with the sshd process, we could first execute the pidof command to view the list of PIDs associated with the process and then loop over the PIDs to display the corresponding VmSwap value from their status files.

[root@linuxnix ~]# pidof sshd
38984 27657
[root@linuxnix ~]# for pid in $(pidof sshd);do grep -i vmswap /proc/$pid/status;done
VmSwap: 0 kB
VmSwap: 0 kB

From the above output we can determine that both PIDs associated with the sshd daemon are not using any swap memory at the moment. To calculate the total usage we could use awk to add the value of the VmSwap parameter column by column as shown in the below command line output.

[root@linuxnix ~]# for i in $(pidof sshd);do grep -i vmsw /proc/$i/status;done | awk '{s+=$2} END {print s}'
0

Method 2: Using pmap command
The pmap command can be used to print the process map for a given process. Given below is an example of executing this command on a process with PID 116538.

[ssuri@linuxnix:~] $ sudo pmap -X 116538
116538: sshd: ssuri@pts/18
Address Perm Offset Device Inode Size Rss Pss Referenced Anonymous Swap Locked Mapping
55c05e3ed000 r-xp 00000000 fd:00 26071592 800 316 13 316 0 0 0 sshd
55c05e6b4000 r--p 000c7000 fd:00 26071592 16 16 8 12 16 0 0 sshd
55c05e6b8000 rw-p 000cb000 fd:00 26071592 4 4 4 4 4 0 0 sshd
55c05e6b9000 rw-p 00000000 00:00 0 36 36 26 24 36 0 0
55c0603f9000 rw-p 00000000 00:00 0 288 192 162 140 192 0 0 [heap]
7f27b7d9f000 r-xp 00000000 fd:00 17113156 12 0 0 0 0 0 0 pam_lastlog.so
7f27b7da2000 ---p 00003000 fd:00 17113156 2044 0 0 0 0 0 0 pam_lastlog.so
7f27b7fa1000 r--p 00002000 fd:00 17113156 4 4 2 0 4 0 0 pam_lastlog.so
7f27b7fa2000 rw-p 00003000 fd:00 17113156 4 4 2 0 4 0 0 pam_lastlog.so
7f27b7fa3000 r-xp 00000000 fd:00 25508146 12 0 0 0 0 0 0 libpam_misc.so.0.82.0
7f27b7fa6000 ---p 00003000 fd:00 25508146 2044 0 0 0 0 0 0 libpam_misc.so.0.82.0
7f27b81a5000 r--p 00002000 fd:00 25508146 4 4 2 0 4 0 0 libpam_misc.so.0.82.0
7f27b81a6000 rw-p 00003000 fd:00 25508146 4 4 2 0 4 0 0 libpam_misc.so.0.82.0
7f27b81a7000 r-xp 00000000 fd:00 17113157 16 0 0 0 0 0 0 pam_limits.so
-----------------------------------------------------------output truncated for brevity

Note that the -X option works on RHEL 7 and not on RHEL 6.

Method 3: using smaps file
The thrid and final technique we will be demonstrating is calculating swap memory used by the process using the smaps file for the process available in /proc/<PID>/ The smaps file contains information about the resources used by each library that the process is using including the swap memory usage. Given below is an example wherin we view the smaps file for a process with the PID 62156 and filter the output to include swap information only and further filter the output to exclude any libraries using 0 KB swap.

[ssuri@linuxnix:~] $ sudo cat /proc/62156/smaps | grep Swap | grep -v 0
Swap: 68 kB
Swap: 4 kB
Swap: 848 kB
Swap: 152 kB
Swap: 368 kB
Swap: 828 kB
Swap: 416 kB
Swap: 68 kB
Swap: 24 kB
Swap: 8 kB
Swap: 132 kB
[ssuri@linuxnix:~] $

To get the consolidated usage for the process we could pipe the above output to an awk command as shown below:

cat /proc/62156/smaps | grep -i swap |awk '{s+=$2} END {print s}'

Conclusion

This concludes our demonstration of three different techniques to obtain information about swap utilization on a per process basis. We hope that you’ve found this article to be useful and we look forward towards your suggestions and feedback.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.