How to display processes using swap space

Identify and print processes using swap space to get a better understanding of the Linux operating system.

Display processes using swap space

Use the following command to simply display processes using swap space. This list will be sorted by process id by default due to a way find command return its results, which are parsed by awk utility.

$ find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \;
[..]
         1 systemd                                       12 kB
       555 systemd-udevd                                 52 kB
       957 AgentSvc                                     448 kB
       966 accounts-daemon                                8 kB
       978 networkd-dispat                              256 kB
       981 dbus-daemon                                   16 kB
       987 cron                                           4 kB
      1134 gdm3                                          28 kB
      1139 lwsmd                                         84 kB
      1270 unattended-upgr                              388 kB
      1471 rtkit-daemon                                   4 kB
      1731 whoopsie                                      12 kB
      1734 kerneloops                                     4 kB
      1868 (sd-pam)                                      12 kB
[..]

Display processes using swap space sorted by used space

Use additional awk instance to add temporary column at the beginning, so data could be easily sorted by used swap space in ascending order.

$ find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \; | awk '{print $(NF-1),$0}' | sort -h | cut -d " " -f2-
[..]
      1471 rtkit-daemon                                   4 kB
      1734 kerneloops                                     4 kB
       987 cron                                           4 kB
       966 accounts-daemon                                8 kB
      1731 whoopsie                                      12 kB
      1868 (sd-pam)                                      12 kB
         1 systemd                                       12 kB
       981 dbus-daemon                                   16 kB
      1134 gdm3                                          28 kB
       555 systemd-udevd                                 52 kB
      1139 lwsmd                                         84 kB
       978 networkd-dispat                              256 kB
      1270 unattended-upgr                              388 kB
       957 AgentSvc                                     448 kB
[..]

Display top ten processes using swap space

The following command will sort processes by used swap space in descending order, then execute head utility to limit number of records.

$ find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") printf "%10s %-30s %20s\n",process["Pid"],process["Name"],process["VmSwap"]}' '{}' \; | awk '{print $(NF-1),$0}' | sort -hr | head | cut -d " " -f2- 
       957 AgentSvc                                     448 kB
      1270 unattended-upgr                              388 kB
       978 networkd-dispat                              256 kB
      1139 lwsmd                                         84 kB
       555 systemd-udevd                                 52 kB
      1134 gdm3                                          28 kB
       981 dbus-daemon                                   16 kB
         1 systemd                                       12 kB
      1868 (sd-pam)                                      12 kB
      1731 whoopsie                                      12 kB

Display top ten processes using swap space with percentage values

Read (see 1st command) or calculate (see 2nd command) total available swap space to calculate and display per process percentage swap usage. Both of these commands are equivalent.

$ find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/meminfo | sed  -n -e "s/^SwapTotal:[ ]*\([0-9]*\) kB/\1/p")" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") {used_swap=process["VmSwap"];sub(/[ a-zA-Z]+/,"",used_swap);percent=(used_swap/TOTSWP*100); printf "%10s %-30s %20s %6.2f%\n",process["Pid"],process["Name"],process["VmSwap"],percent} }' '{}' \;  | awk '{print $(NF-2),$0}' | sort -hr | head | cut -d " " -f2-
$ find /proc -maxdepth 2 -path "/proc/[0-9]*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/swaps | sed 1d | awk 'BEGIN{sum=0} {sum=sum+$(NF-2)} END{print sum}')" '{process[$1]=$2;sub(/^[ \t]+/,"",process[$1]);} END {if(process["VmSwap"] && process["VmSwap"] != "0 kB") {used_swap=process["VmSwap"];sub(/[ a-zA-Z]+/,"",used_swap);percent=(used_swap/TOTSWP*100); printf "%10s %-30s %20s %6.2f%\n",process["Pid"],process["Name"],process["VmSwap"],percent} }' '{}' \;  | awk '{print $(NF-2),$0}' | sort -hr | head | cut -d " " -f2-
      957 AgentSvc                                448 kB   0.02%
     1270 unattended-upgr                         388 kB   0.02%
      978 networkd-dispat                         256 kB   0.02%
     1139 lwsmd                                   84  kB   0.01%
      555 systemd-udevd                           52  kB   0.01%
     1134 gdm3                                    28  kB   0.01%
      981 dbus-daemon                             16  kB   0.01%
        1 systemd                                 12  kB   0.01%
     1868 (sd-pam)                                12  kB   0.01%
     1731 whoopsie                                12  kB   0.01%

awk is beautiful!

Leave a Reply

Your email address will not be published. Required fields are marked *