可以把 /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:
-
becomes ‘
abc\?’ when using extended regular expressions. It matches the literal string ‘
abc?’.
-
becomes ‘
c+’ when using extended regular expressions. It matches one or more ‘
c’s.
-
becomes ‘
a{3,}’ when using extended regular expressions. It matches three or more ‘
a’s.
-
becomes ‘
(abc){2,3}’ when using extended regular expressions. It matches either ‘
abcabc’ or ‘
abcabcabc’.
- becomes ‘ (abc*)\1’ when using extended regular expressions. Back references must still be escaped when using extended regular expressions.
abc?
c\+
a\{3,\}
\(abc\)\{2,3\}
\(abc*\)\1
$ 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 后面跟的参数要用单引号,不可以用双引号。