测试进程占用的虚拟内存大小

可以把 /proc/PID/maps文件里的各项累加起来,取和即是。

$ (echo 'a=0'; sed  -r 's;^([^-]*)-([^- ]*).*;a=$((a+0x\2-0x\1));'  /proc/2451/maps;printf 'echo Calculated mmap size : $((a>>10)) KB' )  | sh -
Calculated mmap size : 92484 KB
另一种方法是读取 /proc/PID/status文件里的VmSize项:

$ echo "size of VM for status: " $(cat /proc/2451/status | grep "VmSize" | sed -r 's;^VmSize:[ \t]*([0-9]*).*;\1;') "KB"
size of VM for status:  92980 KB
稍微解释下。

1. sed 命令的 -r选项表示 使用 extended regular regressions.

下面一段话摘自GNU: http://www.gnu.org/software/sed/manual/html_node/Extended-regexps.html

The only difference between basic and extended regular expressions is in the behavior of a few characters: ‘?’, ‘+’, parentheses,and braces (‘{}’). While basic regular expressions require these to be escaped if you want them to behave as special characters,when using extended regular expressions you must escape them if you want them to match a literal character.

Examples:

abc?
becomes ‘ abc\?’ when using extended regular expressions. It matches the literal string ‘ abc?’.
c\+
becomes ‘ c+’ when using extended regular expressions. It matches one or more ‘ c’s.
a\{3,\}
becomes ‘ a{3,}’ when using extended regular expressions. It matches three or more ‘ a’s.
\(abc\)\{2,3\}
becomes ‘ (abc){2,3}’ when using extended regular expressions. It matches either ‘ abcabc’ or ‘ abcabcabc’.
\(abc*\)\1
becomes ‘ (abc*)\1’ when using extended regular expressions. Back references must still be escaped when using extended regular expressions.
所以,如果不加 -r参数,上面的命令要改成:

$ echo "size of VM for status: " $(cat /proc/2451/status | grep "VmSize" | sed  's;^VmSize:[ \t]*\([0-9]*\).*;\1;') "KB"
size of VM for status:  92980 KB
2.第一个命令里的sed 后面跟的参数要用单引号,不可以用双引号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值