zoukankan      html  css  js  c++  java
  • smem – Linux 下基于进程和用户的内存占用报告--转载

    Linux 系统的内存管理工作中,内存使用情况的监控是十分重要的,在各种 Linux 发行版上你会找到许多这种工具。它们的工作方式多种多样,在这里,我们将会介绍如何安装和使用这样的一个名为 smem 的工具软件。

    Smem 是一款命令行下的内存使用情况报告工具,它能够给用户提供 Linux 系统下的内存使用的多种报告。和其它传统的内存报告工具不同的是,它有个独特的功能,可以报告 PSS(按比例占用大小(Proportional Set Size)),这种内存使用量表示方法对于那些在虚拟内存中的应用和库更有意义。

    已有的传统工具会将目光主要集中于读取 RSS(实际占用大小(Resident Set Size)),这种方法是以物理内存方案来衡量使用情况的标准方法,但是往往高估了应用程序的内存的使用情况。

    PSS 从另一个侧面,通过判定在虚拟内存中的应用和库所使用的“合理分享”的内存,来给出更可信的衡量结果。

    你可以阅读此指南 (关于内存的 RSS 和 PSS)了解 Linux 系统中的内存占用,不过现在让我们继续看看 smem 的特点。

    Smem 这一工具的特点

    • 系统概览列表
    • 以进程、映射和用户来显示或者是过滤
    • 从 /proc 文件系统中得到数据
    • 从多个数据源配置显示的条目
    • 可配置输出单位和百分比
    • 易于配置列表表头和汇总
    • 从镜像文件夹或者是压缩的 tar 文件中获得数据快照
    • 内置的图表生成机制
    • 轻量级的捕获工具,可用于嵌入式系统

    如何安装 Smem - Linux 下的内存使用情况报告工具

    安装之前,需要确保满足以下的条件:

    • 现代内核 (版本号高于 2.6.27)
    • 较新的 Python 版本 (2.4 及以后版本)
    • 可选的 matplotlib 库用于生成图表

    对于当今的大多数的 Linux 发行版而言,内核版本和 Python 的版本都能够满足需要,所以仅需要为生成良好的图表安装 matplotlib 库。

    RHEL, CentOS 和 Fedora

    首先启用 EPEL (Extra Packages for Enterprise Linux) 软件源,然后按照下列步骤操作:

    1. #yum install smem python-matplotlib python-tk

    Debian 和 Ubuntu

    1. $ sudoapt-get install smem

    Linux Mint

    1. $ sudoapt-get install smem python-matplotlib python-tk

    Arch Linux

    使用此 AUR 仓库

    如何使用 Smem – Linux 下的内存使用情况报告工具

    为了查看整个系统所有用户的内存使用情况,运行以下的命令:

    1. $ sudo smem

    监视 Linux 系统中的内存使用情况

    1. PID UserCommandSwap USS PSS RSS
    2. 6367 tecmint cat01001451784
    3. 6368 tecmint cat01001471676
    4. 2864 tecmint /usr/bin/ck-launch-session 01441651780
    5. 7656 tecmint gnome-pty-helper 01561781832
    6. 5758 tecmint gnome-pty-helper 01561791916
    7. 1441 root /sbin/getty -838400 tty2 01521842052
    8. 1434 root /sbin/getty -838400 tty5 01561872060
    9. 1444 root /sbin/getty -838400 tty3 01561872060
    10. 1432 root /sbin/getty -838400 tty4 01561882124
    11. 1452 root /sbin/getty -838400 tty6 01641962064
    12. 2619 root /sbin/getty -838400 tty1 01641962136
    13. 3544 tecmint sh -c /usr/lib/linuxmint/mi 02122241540
    14. 1504 root acpid -c /etc/acpi/events -02202361604
    15. 3311 tecmint syndaemon -i 0.5-K -R 02522922556
    16. 3143 rtkit /usr/lib/rtkit/rtkit-daemon 03003262548
    17. 1588 root cron02923332344
    18. 1589 avahi avahi-daemon:chroot helpe 01243341632
    19. 1523 root /usr/sbin/irqbalance 03163432096
    20. 585 root upstart-socket-bridge --dae 03283511820
    21. 3033 tecmint /usr/bin/dbus-launch --exit03283602160
    22. 1346 root upstart-file-bridge --daemo 03483711776
    23. 2607 root /usr/bin/xdm 01883782368
    24. 1635 kernoops /usr/sbin/kerneloops 03523862684
    25. 344 root upstart-udev-bridge --daemo 04004272132
    26. 2960 tecmint /usr/bin/ssh-agent /usr/bin 0480485992
    27. 3468 tecmint /bin/dbus-daemon --config-f 03445153284
    28. 1559 avahi avahi-daemon: running [tecm 02845173108
    29. 7289 postfix pickup -l -t unix -u -c 02885342808
    30. 2135 root /usr/lib/postfix/master 03525762872
    31. 2436 postfix qmgr -l -t unix -u 03606062884
    32. 1521 root /lib/systemd/systemd-logind 06006503276
    33. 2222 nobody /usr/sbin/dnsmasq --no-reso 06046693288
    34. ....

    当普通用户运行 smem,将会显示由该用户启用的进程的占用情况,其中进程按照 PSS 的值升序排列。

    下面的输出为用户 “aaronkilik” 启用的进程的使用情况:

    1. $ smem

    监视 Linux 系统中的内存使用情况

    1. PID UserCommandSwap USS PSS RSS
    2. 6367 tecmint cat01001451784
    3. 6368 tecmint cat01001471676
    4. 2864 tecmint /usr/bin/ck-launch-session 01441661780
    5. 3544 tecmint sh -c /usr/lib/linuxmint/mi 02122241540
    6. 3311 tecmint syndaemon -i 0.5-K -R 02522922556
    7. 3033 tecmint /usr/bin/dbus-launch --exit03283602160
    8. 3468 tecmint /bin/dbus-daemon --config-f 03445153284
    9. 3122 tecmint /usr/lib/gvfs/gvfsd 06568015552
    10. 3471 tecmint /usr/lib/at-spi2-core/at-sp 07088645992
    11. 3396 tecmint /usr/lib/gvfs/gvfs-mtp-volu 08049146204
    12. 3208 tecmint /usr/lib/x86_64-linux-gnu/i 089210126188
    13. 3380 tecmint /usr/lib/gvfs/gvfs-afc-volu 082010246396
    14. 3034 tecmint //bin/dbus-daemon --fork -- 0 920 1081 3040
    15. 3365 tecmint /usr/lib/gvfs/gvfs-gphoto2-097210996052
    16. 3228 tecmint /usr/lib/gvfs/gvfsd-trash -098011536648
    17. 3107 tecmint /usr/lib/dconf/dconf-servic 0121212835376
    18. 6399 tecmint /opt/google/chrome/chrome -0144140910732
    19. 3478 tecmint /usr/lib/x86_64-linux-gnu/g 0172418206320
    20. 7365 tecmint /usr/lib/gvfs/gvfsd-http --0135218848704
    21. 6937 tecmint /opt/libreoffice5.0/program 0114023285040
    22. 3194 tecmint /usr/lib/x86_64-linux-gnu/p 01956240514228
    23. 6373 tecmint /opt/google/chrome/nacl_hel 0232425418908
    24. 3313 tecmint /usr/lib/gvfs/gvfs-udisks2-0246027548736
    25. 3464 tecmint /usr/lib/at-spi2-core/at-sp 0268428237920
    26. 5771 tecmint ssh-p 4521 tecmnt765@212.70254428646540
    27. 5759 tecmint /bin/bash0241629235640
    28. 3541 tecmint /usr/bin/python /usr/bin/mi 0258430087248
    29. 7657 tecmint bash0251630556028
    30. 3127 tecmint /usr/lib/gvfs/gvfsd-fuse /r 0302431268032
    31. 3205 tecmint mate-screensaver 02520333118072
    32. 3171 tecmint /usr/lib/mate-panel/notific 02860349517140
    33. 3030 tecmint x-session-manager 04400487917500
    34. 3197 tecmint mate-volume-control-applet 03860522623736
    35. ...

    使用 smem 时还有一些参数可以选用,例如当查看整个系统的内存占用情况,运行以下的命令:

    1. $ sudo smem -w

    监视 Linux 系统中的内存使用情况

    1. AreaUsedCacheNoncache
    2. firmware/hardware 000
    3. kernel image 000
    4. kernel dynamic memory 14253201291412133908
    5. userspace memory 22153684516081763760
    6. free memory 442493644249360

    如果想要查看每一个用户的内存使用情况,运行以下的命令:

    1. $ sudo smem -u

    Linux 下以用户为单位监控内存占用情况

    1. UserCountSwap USS PSS RSS
    2. rtkit 103003262548
    3. kernoops 103523852684
    4. avahi 204088514740
    5. postfix 2064811405692
    6. messagebus 10101211733320
    7. syslog 10139614193232
    8. www-data 205100657213580
    9. mpd 107416830212896
    10. nobody 2040241130524728
    11. root 390323876353418496520
    12. tecmint 640165288818156992763112

    你也可以按照映射显示内存使用情况:

    1. $ sudo smem -m

    Linux 下以映射为单位监控内存占用情况

    1. MapPIDs AVGPSS PSS
    2. /dev/fb0 100
    3. /home/tecmint/.cache/fontconfig/7ef2298f1800
    4. /home/tecmint/.cache/fontconfig/c57959a1 1800
    5. /home/tecmint/.local/share/mime/mime.cac 1500
    6. /opt/google/chrome/chrome_material_100_p 900
    7. /opt/google/chrome/chrome_material_200_p 900
    8. /usr/lib/x86_64-linux-gnu/gconv/gconv-mo 4100
    9. /usr/share/icons/Mint-X-Teal/icon-theme.1500
    10. /var/cache/fontconfig/0c9eb80ebd1c36541e2000
    11. /var/cache/fontconfig/0d8c3b2ac0904cb8a52000
    12. /var/cache/fontconfig/1ac9eb803944fde1462000
    13. /var/cache/fontconfig/3830d5c3ddfd5cd38a2000
    14. /var/cache/fontconfig/385c0604a188198f042000
    15. /var/cache/fontconfig/4794a0821666d791902000
    16. /var/cache/fontconfig/56cf4f4769d0f4abc82000
    17. /var/cache/fontconfig/767a8244fc0220cfb52000
    18. /var/cache/fontconfig/8801497958630a81b72000
    19. /var/cache/fontconfig/99e8ed0e538f840c562000
    20. /var/cache/fontconfig/b9d506c9ac06c20b43 2000
    21. /var/cache/fontconfig/c05880de57d1f5e948 2000
    22. /var/cache/fontconfig/dc05db6664285cc2f1 2000
    23. /var/cache/fontconfig/e13b20fdb08344e0e6 2000
    24. /var/cache/fontconfig/e7071f4a29fa870f43 2000
    25. ....

    还有其它的选项可以筛选 smem 的输出,下面将会举两个例子。

    要按照用户名筛选输出的信息,使用 -u 或者是 --userfilter="regex" 选项,就像下面的命令这样:

    1. $ sudo smem -u

    按照用户报告内存使用情况

    1. UserCountSwap USS PSS RSS
    2. rtkit 103003262548
    3. kernoops 103523852684
    4. avahi 204088514740
    5. postfix 2064811405692
    6. messagebus 10101211733320
    7. syslog 10140014233236
    8. www-data 205100657213580
    9. mpd 107416830212896
    10. nobody 2040241130524728
    11. root 390323804353374496552
    12. tecmint 640170890018717662819212

    要按照进程名称筛选输出信息,使用 -P 或者是 --processfilter="regex" 选项,就像下面的命令这样:

    1. $ sudo smem --processfilter="firefox"

    按照进程名称报告内存使用情况

    1. PID UserCommandSwap USS PSS RSS
    2. 9212 root sudo smem --processfilter=f 0117214344856
    3. 9213 root /usr/bin/python /usr/bin/sm 07368779311984
    4. 4424 tecmint /usr/lib/firefox/firefox 0931732937590961504

    输出的格式有时候也很重要,smem 提供了一些帮助您格式化内存使用报告的参数,我们将举出几个例子。

    设置哪些列在报告中,使用 -c 或者是 --columns 选项,就像下面的命令这样:

    1. $ sudo smem -c "name user pss rss"

    按列报告内存使用情况

    1. NameUser PSS RSS
    2. cat tecmint 1451784
    3. cat tecmint 1471676
    4. ck-launch-sessi tecmint 1651780
    5. gnome-pty-helpe tecmint 1781832
    6. gnome-pty-helpe tecmint 1791916
    7. getty root 1842052
    8. getty root 1872060
    9. getty root 1872060
    10. getty root 1882124
    11. getty root 1962064
    12. getty root 1962136
    13. sh tecmint 2241540
    14. acpid root 2361604
    15. syndaemon tecmint 2962560
    16. rtkit-daemon rtkit 3262548
    17. cron root 3332344
    18. avahi-daemon avahi 3341632
    19. irqbalance root 3432096
    20. upstart-socket- root 3511820
    21. dbus-launch tecmint 3602160
    22. upstart-file-br root 3711776
    23. xdm root 3782368
    24. kerneloops kernoops 3862684
    25. upstart-udev-br root 4272132
    26. ssh-agent tecmint 485992
    27. ...

    也可以调用 -p 选项以百分比的形式报告内存使用情况,就像下面的命令这样:

    1. $ sudo smem -p

    按百分比报告内存使用情况

    1. PID UserCommandSwap USS PSS RSS
    2. 6367 tecmint cat0.00%0.00%0.00%0.02%
    3. 6368 tecmint cat0.00%0.00%0.00%0.02%
    4. 9307 tecmint sh -c {sudo/usr/lib/linux 0.00%0.00%0.00%0.02%
    5. 2864 tecmint /usr/bin/ck-launch-session 0.00%0.00%0.00%0.02%
    6. 3544 tecmint sh -c /usr/lib/linuxmint/mi 0.00%0.00%0.00%0.02%
    7. 5758 tecmint gnome-pty-helper 0.00%0.00%0.00%0.02%
    8. 7656 tecmint gnome-pty-helper 0.00%0.00%0.00%0.02%
    9. 1441 root /sbin/getty -838400 tty2 0.00%0.00%0.00%0.03%
    10. 1434 root /sbin/getty -838400 tty5 0.00%0.00%0.00%0.03%
    11. 1444 root /sbin/getty -838400 tty3 0.00%0.00%0.00%0.03%
    12. 1432 root /sbin/getty -838400 tty4 0.00%0.00%0.00%0.03%
    13. 1452 root /sbin/getty -838400 tty6 0.00%0.00%0.00%0.03%
    14. 2619 root /sbin/getty -838400 tty1 0.00%0.00%0.00%0.03%
    15. 1504 root acpid -c /etc/acpi/events -0.00%0.00%0.00%0.02%
    16. 3311 tecmint syndaemon -i 0.5-K -R 0.00%0.00%0.00%0.03%
    17. 3143 rtkit /usr/lib/rtkit/rtkit-daemon 0.00%0.00%0.00%0.03%
    18. 1588 root cron0.00%0.00%0.00%0.03%
    19. 1589 avahi avahi-daemon:chroot helpe 0.00%0.00%0.00%0.02%
    20. 1523 root /usr/sbin/irqbalance 0.00%0.00%0.00%0.03%
    21. 585 root upstart-socket-bridge --dae 0.00%0.00%0.00%0.02%
    22. 3033 tecmint /usr/bin/dbus-launch --exit0.00%0.00%0.00%0.03%
    23. ....

    下面的命令将会在输出的最后输出一行汇总信息:

    1. $ sudo smem -t

    报告内存占用合计

    1. PID UserCommandSwap USS PSS RSS
    2. 6367 tecmint cat01001391784
    3. 6368 tecmint cat01001411676
    4. 9307 tecmint sh -c {sudo/usr/lib/linux 0961581508
    5. 2864 tecmint /usr/bin/ck-launch-session 01441631780
    6. 3544 tecmint sh -c /usr/lib/linuxmint/mi 01081701540
    7. 5758 tecmint gnome-pty-helper 01561761916
    8. 7656 tecmint gnome-pty-helper 01561761832
    9. 1441 root /sbin/getty -838400 tty2 01521812052
    10. 1434 root /sbin/getty -838400 tty5 01561842060
    11. 1444 root /sbin/getty -838400 tty3 01561842060
    12. 1432 root /sbin/getty -838400 tty4 01561852124
    13. 1452 root /sbin/getty -838400 tty6 01641932064
    14. 2619 root /sbin/getty -838400 tty1 01641932136
    15. 1504 root acpid -c /etc/acpi/events -02202321604
    16. 3311 tecmint syndaemon -i 0.5-K -R 02602982564
    17. 3143 rtkit /usr/lib/rtkit/rtkit-daemon 03003242548
    18. 1588 root cron02923262344
    19. 1589 avahi avahi-daemon:chroot helpe 01243321632
    20. 1523 root /usr/sbin/irqbalance 03163402096
    21. 585 root upstart-socket-bridge --dae 03283491820
    22. 3033 tecmint /usr/bin/dbus-launch --exit03283592160
    23. 1346 root upstart-file-bridge --daemo 03483701776
    24. 2607 root /usr/bin/xdm 01883752368
    25. 1635 kernoops /usr/sbin/kerneloops 03523842684
    26. 344 root upstart-udev-bridge --daemo 04004262132
    27. .....
    28. -------------------------------------------------------------------------------
    29. 134110217142823762663587972

    另外,smem 也提供了选项以图形的形式报告内存的使用情况,我们将会在下一小节深入介绍。

    比如,你可以生成一张进程的 PSS 和 RSS 值的条状图。在下面的例子中,我们会生成属于 root 用户的进程的内存占用图。

    纵坐标为每一个进程的 PSS 和 RSS 值,横坐标为 root 用户的所有进程(的 ID):

    1. $ sudo smem --userfilter="root"--bar pid -c"pss rss"

    Linux Memory Usage in PSS and RSS Values

    Linux Memory Usage in PSS and RSS Values

    也可以生成进程及其 PSS 和 RSS 占用量的饼状图。以下的命令将会输出一张 root 用户的所有进程的饼状图。

    --pie name 意思为以各个进程名字为标签,-s 选项用来以 PSS 的值排序。

    1. $ sudo smem --userfilter="root"--pie name -s pss

    Linux Memory Consumption by Processes

    Linux Memory Consumption by Processes

    除了 PSS 和 RSS ,其它的字段也可以用于图表的标签:

    假如需要获得帮助,非常简单,仅需要输入 smem -h 或者是浏览帮助页面。

    关于 smem 的介绍到此为止,不过想要更好的了解它,可以通过 man 手册获得更多的选项,然后一一实践。有什么想法或者疑惑,都可以跟帖评价。

    参考链接: https://www.selenic.com/smem/

  • 相关阅读:
    使用Fiddler捕获Java程序中的HTTP请求
    js解析json对象和json字符串
    写代码 在与思考
    sql 逻辑读取次数
    WbeAPI 学习笔记
    大话设计模式阅读笔记
    行转列
    sql server 的多表查询(left join )
    union 和union all比较
    sql 跨库和域插入数据库
  • 原文地址:https://www.cnblogs.com/xiaoyaojinzhazhadehangcheng/p/14866599.html
Copyright © 2011-2022 走看看