How to find out which processes are using swap space in Linux?

后端 未结 18 1969
终归单人心
终归单人心 2020-11-27 09:11

Under Linux, how do I find out which process is using the swap space more?

18条回答
  •  抹茶落季
    2020-11-27 09:14

    I adapted a different script on the web to this long one-liner:

     { date;for f in /proc/[0-9]*/status; do 
       awk '{k[$1]=$2} END { if (k["VmSwap:"]) print k["Pid:"],k["Name:"],k["VmSwap:"];}' $f 2>/dev/null; 
       done | sort -n ; }
    

    Which I then throw into a cronjob and redirect output to a logfile. The information here is the same as accumulating the Swap: entries in the smaps file, but if you want to be sure, you can use:

    { date;for m in /proc/*/smaps;do 
      awk '/^Swap/ {s+=$2} END { if (s) print FILENAME,s }' $m 2>/dev/null;
      done | tr -dc ' [0-9]\n' |sort -k 1n; }
    

    The output of this version is in two columns: pid, swap amount. In the above version, the tr strips the non-numeric components. In both cases, the output is sorted numerically by pid.

提交回复
热议问题