侧边栏壁纸
  • 累计撰写 119 篇文章
  • 累计创建 25 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Linux统计进程占用内存的神器

梁来福
2022-08-25 / 0 评论 / 0 点赞 / 1 阅读 / 13095 字
温馨提示:
本文最后更新于 2024-05-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、简介

smem是一个工具,可以提供大量关于 Linux 系统内存使用情况的报告。与现有工具不同,smem 可以报告比例集大小 (PSS),它更有意义地表示虚拟内存系统中库和应用程序使用的内存量。

由于大部分物理内存通常在多个应用程序之间共享,因此称为常驻集大小 (RSS) 的内存使用标准度量将大大高估内存使用。相反,PSS 衡量每个应用程序在每个共享区域中的公平份额,以给出一个现实的衡量标准。

2、Smem功能

  • 系统概览列表
  • 按进程、映射、用户输出
  • 按进程、映射或用户过滤输出
  • 来自多个数据源的可配置列
  • 可配置的输出单位和百分比
  • 可配置的标题和总计
  • 从/proc读取实时数据
  • 从目录镜像或压缩 tarball 中读取数据快照
  • 用于嵌入式系统的轻量级捕获工具
  • 内置图表生成

3、安装

3.1 RHEL/CentOS

[root@Laifu-Blog ~]#yum install smem python-matplotlib -y

3.2 Fedora

$ sudo dnf install smem
$ sudo dnf install python-matplotlib

3.3 Debian/Ubuntu

$ sudo apt-get install smem
$ sudo apt-get install python-matplotlib

4、使用

4.1 不带任何参数

安装完成后,只需在终端上触发以下命令即可测量内存使用情况

[root@Laifu-Blog ~]#smem
  PID User     Command                         Swap      USS      PSS      RSS
14730 root     sleep 60                           0      124      145      648
 1385 root     /sbin/agetty --keep-baud 11        0      140      166      864
 1386 root     /sbin/agetty --noclear tty1        0      144      169      856
  655 root     /usr/sbin/acpid                    0      148      179      532
  659 libstoragemgmt /usr/bin/lsmd -d                   0      184      205      848
 1171 root     /usr/sbin/atd -f                   0      232      264     1052
  405 root     /usr/sbin/lvmetad -f               0      324      360     1288
  990 root     /usr/sbin/rshim                    0      340      368      956
  555 root     /sbin/auditd                       0      528      551     1088
16198 mysql    /bin/sh /usr/bin/mysqld_saf        0      460      684     1884
 1174 root     /usr/sbin/crond -n                 0      676      713     1628
27854 root     nginx: master process /usr/        0      376      812     2408
 1480 root     /usr/local/qcloud/stargate/        0      776      816     1688
 1204 root     /bin/bash /usr/local/sa/age        0      604      826     2020
  413 root     /usr/lib/systemd/systemd-ud        0      780      835     1848
  654 ntp      /usr/sbin/ntpd -u ntp:ntp -        0      900     1023     2360
  650 dbus     /usr/bin/dbus-daemon --syst        0      872     1040     2408
  644 root     /usr/lib/systemd/systemd-lo        0     1124     1158     2104
 1431 root     /usr/sbin/sshd -D                  0     1040     1765     4360
  922 root     /sbin/dhclient -q -lf /var/        0     2084     2087     2268
··········

4.2 以MB为单位显示输出

默认情况下,内存使用输出显示KB可能会混淆的内容,添加 k 选项和 smem 以获取输出MB

[root@Laifu-Blog ~]#smem -k
  PID User     Command                         Swap      USS      PSS      RSS
17173 root     sleep 60                           0   120.0K   141.0K   644.0K
 1385 root     /sbin/agetty --keep-baud 11        0   140.0K   167.0K   864.0K
 1386 root     /sbin/agetty --noclear tty1        0   144.0K   169.0K   856.0K
  655 root     /usr/sbin/acpid                    0   148.0K   190.0K   532.0K
  659 libstoragemgmt /usr/bin/lsmd -d                   0   184.0K   205.0K   848.0K
 1171 root     /usr/sbin/atd -f                   0   232.0K   265.0K     1.0M
  405 root     /usr/sbin/lvmetad -f               0   324.0K   362.0K     1.3M
  990 root     /usr/sbin/rshim                    0   340.0K   368.0K   956.0K
  555 root     /sbin/auditd                       0   528.0K   551.0K     1.1M
16198 mysql    /bin/sh /usr/bin/mysqld_saf        0   460.0K   684.0K     1.8M
 1174 root     /usr/sbin/crond -n                 0   676.0K   714.0K     1.6M
27854 root     nginx: master process /usr/        0   376.0K   813.0K     2.4M
 1480 root     /usr/local/qcloud/stargate/        0   776.0K   816.0K     1.6M
 1204 root     /bin/bash /usr/local/sa/age        0   604.0K   827.0K     2.0M
  413 root     /usr/lib/systemd/systemd-ud        0   780.0K   839.0K     1.8M
  654 ntp      /usr/sbin/ntpd -u ntp:ntp -        0   900.0K     1.0M     2.3M
  650 dbus     /usr/bin/dbus-daemon --syst        0   888.0K     1.0M     2.4M
  644 root     /usr/lib/systemd/systemd-lo        0     1.1M     1.1M     2.1M
 1431 root     /usr/sbin/sshd -D                  0     1.0M     1.8M     4.3M
  922 root     /sbin/dhclient -q -lf /var/        0     2.0M     2.0M     2.2M
14665 root     -bash                              0     1.9M     2.2M     3.4M
    1 root     /usr/lib/systemd/systemd --        0     2.3M     2.5M     3.7M
14663 root     sshd: root@pts/0                   0     2.0M     2.9M     5.6M
26098 root     barad_agent                        0     4.2M     5.3M     7.4M
 3600 root     /usr/local/qcloud/tat_agent        0     5.6M     5.6M     5.6
·····················

4.3 显示总内存使用情况

要使用 smem 获取总内存,请添加t选项

[root@Laifu-Blog ~]#smem -kt
  PID User     Command                         Swap      USS      PSS      RSS
17895 root     sleep 60                           0   128.0K   149.0K   652.0K
 1385 root     /sbin/agetty --keep-baud 11        0   140.0K   167.0K   864.0K
 1386 root     /sbin/agetty --noclear tty1        0   144.0K   169.0K   856.0K
  655 root     /usr/sbin/acpid                    0   148.0K   190.0K   532.0K
  659 libstoragemgmt /usr/bin/lsmd -d                   0   184.0K   205.0K   848.0K
 1171 root     /usr/sbin/atd -f                   0   232.0K   265.0K     1.0M
  405 root     /usr/sbin/lvmetad -f               0   324.0K   362.0K     1.3M
  990 root     /usr/sbin/rshim                    0   340.0K   368.0K   956.0K
  555 root     /sbin/auditd                       0   528.0K   551.0K     1.1M
16198 mysql    /bin/sh /usr/bin/mysqld_saf        0   460.0K   684.0K     1.8M
 1174 root     /usr/sbin/crond -n                 0   676.0K   714.0K     1.6M
27854 root     nginx: master process /usr/        0   376.0K   813.0K     2.4M
 1480 root     /usr/local/qcloud/stargate/        0   776.0K   816.0K     1.6M
 1204 root     /bin/bash /usr/local/sa/age        0   604.0K   827.0K     2.0M
  413 root     /usr/lib/systemd/systemd-ud        0   780.0K   839.0K     1.8M
  654 ntp      /usr/sbin/ntpd -u ntp:ntp -        0   900.0K     1.0M     2.3M
  650 dbus     /usr/bin/dbus-daemon --syst        0   888.0K     1.0M     2.4M
  644 root     /usr/lib/systemd/systemd-lo        0     1.1M     1.1M     2.1M
 1431 root     /usr/sbin/sshd -D                  0     1.0M     1.8M     4.3M
  922 root     /sbin/dhclient -q -lf /var/        0     2.0M     2.0M     2.2M
14665 root     -bash                              0     1.9M     2.2M     3.4M
    1 root     /usr/lib/systemd/systemd --        0     2.3M     2.5M     3.7M
14663 root     sshd: root@pts/0                   0     2.0M     2.9M     5.6M
26098 root     barad_agent                        0     4.2M     5.3M     7.4M
··························

4.4 按用户展示内存使用情况

我们可以根据用户对内存使用情况进行排序

[root@Laifu-Blog ~]#smem -uk
User     Count     Swap      USS      PSS      RSS
libstoragemgmt     1        0   184.0K   205.0K   848.0K
ntp          1        0   900.0K     1.0M     2.3M
dbus         1        0   888.0K     1.0M     2.4M
polkitd      1        0     7.9M     8.2M    10.0M
mysql        2        0   122.0M   122.4M   125.1M
root        38        0   616.7M   659.0M   767.6M

4.5 报告系统范围的内存使用情况

[root@Laifu-Blog ~]#smem -wk
Area                           Used      Cache   Noncache
firmware/hardware                 0          0          0
kernel image                      0          0          0
kernel dynamic memory          1.0G     985.4M      74.7M
userspace memory             789.5M     100.4M     689.0M
free memory                  149.3M     149.3M          0

4.6 以百分比显示内存使用情况

[root@Laifu-Blog ~]#smem -p
  PID User     Command                         Swap      USS      PSS      RSS
19360 root     sleep 60                         N/A    0.01%    0.01%    0.03%
 1385 root     /sbin/agetty --keep-baud 11      N/A    0.01%    0.01%    0.04%
 1386 root     /sbin/agetty --noclear tty1      N/A    0.01%    0.01%    0.04%
  655 root     /usr/sbin/acpid                  N/A    0.01%    0.01%    0.03%
  659 libstoragemgmt /usr/bin/lsmd -d                 N/A    0.01%    0.01%    0.04%
 1171 root     /usr/sbin/atd -f                 N/A    0.01%    0.01%    0.05%
  405 root     /usr/sbin/lvmetad -f             N/A    0.02%    0.02%    0.06%
  990 root     /usr/sbin/rshim                  N/A    0.02%    0.02%    0.05%
  555 root     /sbin/auditd                     N/A    0.03%    0.03%    0.05%
16198 mysql    /bin/sh /usr/bin/mysqld_saf      N/A    0.02%    0.03%    0.09%
 1174 root     /usr/sbin/crond -n               N/A    0.03%    0.03%    0.08%
27854 root     nginx: master process /usr/      N/A    0.02%    0.04%    0.12%
 1480 root     /usr/local/qcloud/stargate/      N/A    0.04%    0.04%    0.08%
 1204 root     /bin/bash /usr/local/sa/age      N/A    0.03%    0.04%    0.10%
  413 root     /usr/lib/systemd/systemd-ud      N/A    0.04%    0.04%    0.09%
  654 ntp      /usr/sbin/ntpd -u ntp:ntp -      N/A    0.04%    0.05%    0.12%
  650 dbus     /usr/bin/dbus-daemon --syst      N/A    0.04%    0.05%    0.12%
  644 root     /usr/lib/systemd/systemd-lo      N/A    0.05%    0.06%    0.10%
 1431 root     /usr/sbin/sshd -D                N/A    0.05%    0.09%    0.21%
  922 root     /sbin/dhclient -q -lf /var/      N/A    0.10%    0.10%    0.11%
14665 root     -bash                            N/A    0.10%    0.11%    0.17%
    1 root     /usr/lib/systemd/systemd --      N/A    0.12%    0.12%    0.19%
14663 root     sshd: root@pts/0                 N/A    0.10%    0.14%    0.28%
26098 root     barad_agent                      N/A    0.21%    0.26%    0.37%
····················

4.7 smem数值输出

[root@Laifu-Blog ~]#smem -nk
  PID User     Command                         Swap      USS      PSS      RSS
19837 0        sleep 60                           0   124.0K   145.0K   648.0K
 1385 0        /sbin/agetty --keep-baud 11        0   140.0K   167.0K   864.0K
 1386 0        /sbin/agetty --noclear tty1        0   144.0K   169.0K   856.0K
  655 0        /usr/sbin/acpid                    0   148.0K   190.0K   532.0K
  659 998      /usr/bin/lsmd -d                   0   184.0K   205.0K   848.0K
 1171 0        /usr/sbin/atd -f                   0   232.0K   265.0K     1.0M
  405 0        /usr/sbin/lvmetad -f               0   324.0K   362.0K     1.3M
  990 0        /usr/sbin/rshim                    0   340.0K   368.0K   956.0K
  555 0        /sbin/auditd                       0   528.0K   551.0K     1.1M
16198 27       /bin/sh /usr/bin/mysqld_saf        0   460.0K   684.0K     1.8M
 1174 0        /usr/sbin/crond -n                 0   676.0K   714.0K     1.6M
27854 0        nginx: master process /usr/        0   376.0K   813.0K     2.4M
 1480 0        /usr/local/qcloud/stargate/        0   776.0K   816.0K     1.6M
 1204 0        /bin/bash /usr/local/sa/age        0   604.0K   827.0K     2.0M
  413 0        /usr/lib/systemd/systemd-ud        0   780.0K   839.0K     1.8M
  654 38       /usr/sbin/ntpd -u ntp:ntp -        0   900.0K     1.0M     2.3M
  650 81       /usr/bin/dbus-daemon --syst        0   888.0K     1.0M     2.4M
  644 0        /usr/lib/systemd/systemd-lo        0     1.1M     1.1M     2.1M
 1431 0        /usr/sbin/sshd -D                  0     1.0M     1.8M     4.3M
  922 0        /sbin/dhclient -q -lf /var/        0     2.0M     2.0M     2.2M
14665 0        -bash                              0     1.9M     2.2M     3.4M
    1 0        /usr/lib/systemd/systemd --        0     2.3M     2.5M     3.7M
14663 0        sshd: root@pts/0                   0     2.0M     2.9M     5.6M
26098 0        barad_agent                        0     4.2M     5.3M     7.4M
···················

4.8 smem生成饼图图形输出

可以使用 smem 以饼图的形式查看内存使用情况(USS、PSS 和 RSS)

[root@Laifu-Blog ~]#smem --pie name -s uss

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
博主关闭了所有页面的评论