Linuxでプログラムごとの正確なコアメモリ使用量を表示させる『ps_mem』コマンド

今回は、プログラムごとのコアメモリ使用量を出力するコマンド『ps_mem』を紹介する。

1.インストール

まずはインストールから。
以下のコマンドを実行する。

RHEL系

sudo yum install ps_mem -y

Debian/Ubuntu系

git clone https://github.com/pixelb/ps_mem/
sudo cp ./ps_mem/ps_mem.py /sbin/ps_mem

以上。

2.コマンドの実行

それでは、実際にコマンドを実行してみよう。

$ ps_mem
 Private  +   Shared  =  RAM used       Program

 96.0 KiB +  19.5 KiB = 115.5 KiB       portreserve
128.0 KiB +  28.5 KiB = 156.5 KiB       acpid
140.0 KiB +  17.0 KiB = 157.0 KiB       fcoemon
156.0 KiB +  12.0 KiB = 168.0 KiB       mcelog
164.0 KiB +  43.5 KiB = 207.5 KiB       hald-addon-acpi
152.0 KiB +  60.0 KiB = 212.0 KiB       abrtd
184.0 KiB +  51.5 KiB = 235.5 KiB       hald-addon-input
204.0 KiB +  33.5 KiB = 237.5 KiB       atd
200.0 KiB +  54.5 KiB = 254.5 KiB       hald-addon-storage
224.0 KiB +  46.0 KiB = 270.0 KiB       hald-runner
232.0 KiB +  51.5 KiB = 283.5 KiB       rtkit-daemon
252.0 KiB +  37.5 KiB = 289.5 KiB       dbus-launch
244.0 KiB +  55.5 KiB = 299.5 KiB       anacron
248.0 KiB +  69.0 KiB = 317.0 KiB       abrt-dump-oops
296.0 KiB +  75.5 KiB = 371.5 KiB       rpcbind
360.0 KiB +  37.5 KiB = 397.5 KiB       auditd
344.0 KiB +  55.5 KiB = 399.5 KiB       wpa_supplicant
388.0 KiB +  45.0 KiB = 433.0 KiB       lldpad
356.0 KiB +  97.5 KiB = 453.5 KiB       mingetty (5)
388.0 KiB + 107.0 KiB = 495.0 KiB       gvfsd
416.0 KiB + 114.5 KiB = 530.5 KiB       gdm-binary
416.0 KiB + 119.0 KiB = 535.0 KiB       gdm-session-worker
560.0 KiB +  34.5 KiB = 594.5 KiB       nmon
528.0 KiB +  84.5 KiB = 612.5 KiB       rpc.statd
556.0 KiB +  62.0 KiB = 618.0 KiB       init
604.0 KiB +  58.0 KiB = 662.0 KiB       vsftpd
648.0 KiB +  43.5 KiB = 691.5 KiB       crond
452.0 KiB + 251.0 KiB = 703.0 KiB       avahi-daemon (2)
640.0 KiB + 188.5 KiB = 828.5 KiB       devkit-power-daemon
792.0 KiB + 124.5 KiB = 916.5 KiB       modem-manager
876.0 KiB +  50.0 KiB = 926.0 KiB       pcscd
268.0 KiB + 678.0 KiB = 946.0 KiB       udevd (3)
732.0 KiB + 253.0 KiB = 985.0 KiB       gdm-simple-slave
768.0 KiB + 262.5 KiB =   1.0 MiB       bonobo-activation-server
880.0 KiB + 177.0 KiB =   1.0 MiB       ntpd
  1.1 MiB +  46.5 KiB =   1.1 MiB       bash
992.0 KiB + 373.0 KiB =   1.3 MiB       at-spi-registryd
  1.1 MiB + 334.5 KiB =   1.4 MiB       dbus-daemon (2)
  1.0 MiB + 419.0 KiB =   1.4 MiB       master
  1.0 MiB + 416.0 KiB =   1.4 MiB       pickup
  1.1 MiB + 416.0 KiB =   1.5 MiB       qmgr
  1.4 MiB +  61.0 KiB =   1.5 MiB       rsyslogd
  1.4 MiB + 216.5 KiB =   1.6 MiB       console-kit-daemon
  1.5 MiB + 312.0 KiB =   1.8 MiB       polkitd
  1.6 MiB + 212.0 KiB =   1.8 MiB       pulseaudio
  1.6 MiB + 277.5 KiB =   1.9 MiB       cupsd
  2.2 MiB +  87.0 KiB =   2.3 MiB       hald
  1.6 MiB + 784.0 KiB =   2.4 MiB       polkit-gnome-authentication-agent-1
  2.2 MiB + 309.0 KiB =   2.5 MiB       NetworkManager
  1.8 MiB + 862.5 KiB =   2.7 MiB       gnome-session
  2.8 MiB + 129.0 KiB =   3.0 MiB       automount
  2.0 MiB +   1.0 MiB =   3.0 MiB       gnome-power-manager
  2.8 MiB + 222.5 KiB =   3.1 MiB       qpidd
  2.5 MiB + 632.5 KiB =   3.1 MiB       sshd (2)
  3.4 MiB + 167.0 KiB =   3.6 MiB       gconfd-2
  3.0 MiB +   1.1 MiB =   4.1 MiB       metacity
  3.4 MiB +   2.2 MiB =   5.6 MiB       gnome-settings-daemon
  7.0 MiB +   2.0 MiB =   9.0 MiB       gdm-simple-greeter
 17.4 MiB + 752.5 KiB =  18.1 MiB       Xorg
---------------------------------
                         96.1 MiB
=================================

各プログラムの実行コマンドを表示させる場合は、「-s」オプションを付与する。

$ ps_mem -s
 Private  +   Shared  =  RAM used       Program

 68.0 KiB +  19.5 KiB =  87.5 KiB       /sbin/mingetty /dev/tty4
 72.0 KiB +  19.5 KiB =  91.5 KiB       /sbin/mingetty /dev/tty3
 72.0 KiB +  19.5 KiB =  91.5 KiB       /sbin/mingetty /dev/tty5
 72.0 KiB +  19.5 KiB =  91.5 KiB       /sbin/mingetty /dev/tty6
 72.0 KiB +  19.5 KiB =  91.5 KiB       /sbin/mingetty /dev/tty2
 96.0 KiB +  19.5 KiB = 115.5 KiB       /sbin/portreserve
128.0 KiB +  28.5 KiB = 156.5 KiB       /usr/sbin/acpid
140.0 KiB +  17.0 KiB = 157.0 KiB       /usr/sbin/fcoemon --syslog
156.0 KiB +  12.0 KiB = 168.0 KiB       /usr/sbin/mcelog --daemon
 96.0 KiB +  97.5 KiB = 193.5 KiB       avahi-daemon: chroot helper
164.0 KiB +  43.5 KiB = 207.5 KiB       hald-addon-acpi: listening on acpid socket /var/run/acpid.socket
152.0 KiB +  60.0 KiB = 212.0 KiB       /usr/sbin/abrtd
184.0 KiB +  51.5 KiB = 235.5 KiB       hald-addon-input: Listening on /dev/input/event2 /dev/input/event0
204.0 KiB +  33.5 KiB = 237.5 KiB       /usr/sbin/atd
200.0 KiB +  54.5 KiB = 254.5 KiB       hald-addon-storage: polling /dev/sr0 (every 2 sec)
224.0 KiB +  46.0 KiB = 270.0 KiB       hald-runner
232.0 KiB +  51.5 KiB = 283.5 KiB       /usr/libexec/rtkit-daemon
252.0 KiB +  37.5 KiB = 289.5 KiB       /usr/bin/dbus-launch --exit-with-session
244.0 KiB +  55.5 KiB = 299.5 KiB       /usr/sbin/anacron -s
248.0 KiB +  69.0 KiB = 317.0 KiB       abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages
296.0 KiB +  75.5 KiB = 371.5 KiB       rpcbind
360.0 KiB +  37.5 KiB = 397.5 KiB       auditd
344.0 KiB +  55.5 KiB = 399.5 KiB       /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -B -u -f /var/log/wpa_supplicant.log -P /var/run/wpa_supplicant.pid
388.0 KiB +  45.0 KiB = 433.0 KiB       lldpad -d
388.0 KiB + 107.0 KiB = 495.0 KiB       /usr/libexec/gvfsd
344.0 KiB + 165.0 KiB = 509.0 KiB       /bin/dbus-daemon --fork --print-pid 5 --print-address 7 --session
356.0 KiB + 153.5 KiB = 509.5 KiB       avahi-daemon: running [test-centos.local]
416.0 KiB + 114.5 KiB = 530.5 KiB       /usr/sbin/gdm-binary -nodaemon
416.0 KiB + 119.0 KiB = 535.0 KiB       pam: gdm-password
560.0 KiB +  34.5 KiB = 594.5 KiB       /usr/bin/nmon -f -t -m /var/log/nmon
528.0 KiB +  84.5 KiB = 612.5 KiB       rpc.statd
556.0 KiB +  62.0 KiB = 618.0 KiB       /sbin/init
604.0 KiB +  58.0 KiB = 662.0 KiB       /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
648.0 KiB +  43.5 KiB = 691.5 KiB       crond
640.0 KiB + 188.5 KiB = 828.5 KiB       /usr/libexec/devkit-power-daemon
712.0 KiB + 148.0 KiB = 860.0 KiB       /usr/sbin/sshd
792.0 KiB + 124.5 KiB = 916.5 KiB       /usr/sbin/modem-manager
748.0 KiB + 169.5 KiB = 917.5 KiB       dbus-daemon --system
876.0 KiB +  50.0 KiB = 926.0 KiB       pcscd
268.0 KiB + 678.0 KiB = 946.0 KiB       /sbin/udevd -d (3)
732.0 KiB + 253.0 KiB = 985.0 KiB       /usr/libexec/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1 --force-active-vt
768.0 KiB + 262.5 KiB =   1.0 MiB       /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=12
880.0 KiB + 177.0 KiB =   1.0 MiB       ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
  1.1 MiB +  46.5 KiB =   1.1 MiB       -bash
992.0 KiB + 373.0 KiB =   1.3 MiB       /usr/libexec/at-spi-registryd
  1.0 MiB + 419.0 KiB =   1.4 MiB       /usr/libexec/postfix/master
  1.0 MiB + 416.0 KiB =   1.4 MiB       pickup -l -t fifo -u
  1.1 MiB + 416.0 KiB =   1.5 MiB       qmgr -l -t fifo -u
  1.4 MiB +  61.0 KiB =   1.5 MiB       /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
  1.4 MiB + 216.5 KiB =   1.6 MiB       /usr/sbin/console-kit-daemon --no-daemon
  1.5 MiB + 312.0 KiB =   1.8 MiB       /usr/libexec/polkit-1/polkitd
  1.6 MiB + 212.0 KiB =   1.8 MiB       /usr/bin/pulseaudio --start --log-target=syslog
  1.6 MiB + 277.5 KiB =   1.9 MiB       cupsd -C /etc/cups/cupsd.conf
  1.8 MiB + 484.5 KiB =   2.3 MiB       sshd: root@pts/0
  2.2 MiB +  87.0 KiB =   2.3 MiB       hald
  1.6 MiB + 784.0 KiB =   2.4 MiB       /usr/libexec/polkit-gnome-authentication-agent-1
  2.2 MiB + 309.0 KiB =   2.5 MiB       NetworkManager --pid-file=/var/run/NetworkManager/NetworkManager.pid
  1.8 MiB + 862.5 KiB =   2.7 MiB       /usr/bin/gnome-session --autostart=/usr/share/gdm/autostart/LoginWindow/
  2.8 MiB + 129.0 KiB =   3.0 MiB       automount --pid-file /var/run/autofs.pid
  2.0 MiB +   1.0 MiB =   3.0 MiB       gnome-power-manager
  2.8 MiB + 222.5 KiB =   3.1 MiB       /usr/sbin/qpidd --data-dir /var/lib/qpidd --daemon
  3.4 MiB + 167.0 KiB =   3.6 MiB       /usr/libexec/gconfd-2
  3.0 MiB +   1.1 MiB =   4.1 MiB       metacity
  3.4 MiB +   2.2 MiB =   5.6 MiB       /usr/libexec/gnome-settings-daemon --gconf-prefix=/apps/gdm/simple-greeter/settings-manager-plugins
  7.0 MiB +   2.0 MiB =   9.0 MiB       /usr/libexec/gdm-simple-greeter
 17.4 MiB + 752.5 KiB =  18.1 MiB       /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-l93Kca/database -nolisten tcp vt1
---------------------------------
                         96.1 MiB
=================================

その他、「-w <実行間隔(秒)>」を指定することで定期的にコマンドを実行させることができるようになる。
標準出力を別ファイルに書き出させ、ログとして利用することも可能だろう。