How To Check Swap Usage of Each Processes (文档 ID 1931980.1)
GOAL
This document shows how to check swap usage of each processes, answering "How can I know what process would consume the largest swap space?"
SOLUTION
For all kernels:
Swap usage is shown as "Swap" columns in /proc/<PID>/smaps. This indicates swapped memory size of the each memory ranges, thus the sum of all of these values shows the total swap usage of the process.
For example, the simple script below calculates the total swap usage of the process which PID = 1027:
# echo $((`grep Swap /proc/1027/smaps | awk '{ print $2; }' | tr '\n' '+'`0))
For UEK2(2.6.39) or later kernels:
It is easier to check the swap usage of a specific process, just check "VmSwap" column in /proc/<PID>/status. This indicates the total usage of the swap space as one value. This column does not exist in /proc/<PID>/status with kernel < 2.6.39.
For example, for sorting processes with the swap usage from larger, run a script below:
# for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | more
The value which is shown/calculated in this document does not indicate accurate size of used swap space, but just indicates rough approximate one. Some(like shared library) could be double counted, and others(like swap cache) are not really swapped.