The top and free command display the total amount of free and used physical and swap memory in the server. How do I determine which process is using swap space under Linux operating systems? How do I find out swap space usage of particular process such as memcached?
You can use the any one of the following techniques but keep in mind that because of shared pages, there is no reliable way to get this information.
[a] /proc/meminfo – This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel.
[b] /proc/${PID}/smaps, /proc/${PID}/status, and /proc/${PID}/stat : Use these files to find information about memory, pages and swap used by each process using its PID.
[c] smem – This command (python script) reports memory usage with shared memory divided proportionally.
Finding out process ID and swap usage
Type the following pidof command to find the process ID of a running program called memcached:
pidof memcached
Alternatively, use pgrep command to lookup process PID, enter:
pgrep memcached
Sample outputs (note down PID number #1):
48440
To see swap space used by memcached (PID # 48440), enter (number #2):
48440/status
Listing all process swap space usage
Type the following bash for loop command to see swap space usage per process:
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done
Type the following command to sort out output:
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r
Leave a Reply