-
使用
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
输出显示
34751: /usr/local/python/bin/python /opt/demo/bin/demo-server --config-file=/etc/demo.conf --log-config=/etc/demo-log.conf
Address Kbytes RSS Dirty Mode Mapping
0000000000400000 4 4 0 r-x-- python2.7
0000000000600000 4 4 4 rw--- python2.7
0000000000601000 10096 9964 9964 rw--- [ anon ]
...
00007fff4bb42000 4 4 0 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------ ------ ------
total kB 192320 16360 12512
pmap
是从 /proc/PID/maps
和 /proc/PID/smaps
中获取的信息。
ps
的参数--sort spec
可以指定列进行升序和降序排列
--sort spec specify sorting order. Sorting syntax is [+|-]key[,[+|-]key[,...]]
Choose a multi-letter key from the STANDARD FORMAT SPECIFIERS section.
The "+" is optional since default direction is increasing numerical or
lexicographic order. Identical to k. For example:
ps jax --sort=uid,-ppid,+pid
例如要检查系统中进程使用内存从高到低排列
ps aux --sort -rss
MAX_CPU=5
MAX_MEM=10
ps aux | sort -rn -k 3 | awk '$3>$MAX_CPU {ot="";i=11;while(i<=NF) {ot=ot" "$i;i++};print $3,ot}'
ps aux | sort -rn -k 4 | awk '$3>$MAX_MEM {ot="";i=11;while(i<=NF) {ot=ot" "$i;i++};print $4,ot}'
上述统计超过5%的CPU usage的进程,还存在一个缺陷,就是没有计算
system
,待完善。