生产系统技术支撑中经常接到SWAP使用率超标的告警,可通过以下命令查看是哪些程序占用了SWAP。

1
2
3
4
5
6
7
8
9
for i in $(ls /proc | grep "^[0-9]" | awk '$0>100'); do 
if [ -f "/proc/$i/smaps" ]; then
swap=$(awk '/Swap:/{a=a+$2}END{print a/1024"M"}' /proc/$i/smaps)
#proc_name=$(ps -p $i -o comm= 2>/dev/null)
cmd_line=$(ps -p $i -o args= 2>/dev/null)
echo "$i $swap $proc_name $cmd_line"
fi
done | sort -k2nr | head

返回的数据如下:

1
2
3
4
5
6
7
8
9
10
11
2203 22.0664M  /usr/bin/gnome-shell
899 18.2031M /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
1264 10.5156M /usr/bin/python2 -Es /usr/sbin/tuned -l -P
2276 6.59375M /usr/libexec/geoclue -t 5
2476 6.51562M /usr/bin/gnome-software --gapplication-service
1343 6.45312M /usr/bin/X :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-3jlvD6/database -seat seat0 -nolisten tcp vt1
1278 5.88672M /usr/sbin/libvirtd
2293 5.55078M /usr/libexec/goa-daemon
826 4.78516M /usr/lib/polkit-1/polkitd --no-debug
2616 3.98438M /usr/libexec/evolution-addressbook-factory

  • 第1列是pid
  • 第2列是占用SWAP的大小
  • 第3列是进程的命令行

贴一张数据库生产系统中运行的效果图: