检查进程的内存使用

检查单个进程内存使用

  • 使用 top -p PID 可以查看单个进程的使用,然后按下 d 输入一个代表采样间隔的秒,就可以按照指定是时间刷新。

  • 使用pmap可以详细输出一个进程或线程的内存映射报告

sudo pmap 34751

可以看到详细的报告

34751:   /usr/local/python/bin/python /opt/demo/bin/demo-server --config-file=/etc/demo.conf --log-config=/etc/demo-log.conf
0000000000400000      4K r-x--  /usr/local/python/bin/python2.7
0000000000600000      4K rw---  /usr/local/python/bin/python2.7
0000000000601000  10096K rw---    [ anon ]
0000000041826000      4K -----    [ anon ]
...
00007fff4bab8000     84K rw---    [ stack ]
00007fff4bb42000      4K r-x--    [ anon ]
ffffffffff600000      4K r-x--    [ anon ]
 total           192320K

其中最后输出的total字段值不是进程使用的当前内存,需要使用-x参数来输出扩展字段

-x   extended       Show the extended format.
-d   device         Show the device format.

EXTENDED AND DEVICE FORMAT FIELDS
       Address:   start address of map
       Kbytes:    size of map in kilobytes
       RSS:       resident set size in kilobytes
       Dirty:     dirty pages (both shared and private) in kilobytes
       Mode:      permissions on map: read, write, execute, shared, private (copy on write)
       Mapping:   file backing the map, or ’[ anon ]’ for allocated memory, or  ’[ stack ]’ for the program stack
       Offset:    offset into the file
       Device:    device name (major:minor)

例如 sudo pmap -x 34751 输出显示

pmap是从 /proc/PID/maps/proc/PID/smaps 中获取的信息。

检查进程的内存使用并按照内存使用量排序

ps的参数--sort spec可以指定列进行升序和降序排列

例如要检查系统中进程使用内存从高到低排列

检查系统进程中超过cpu阀值和memory阀值的方法

上述统计超过5%的CPU usage的进程,还存在一个缺陷,就是没有计算system,待完善。

参考

Last updated

Was this helpful?