您的位置:首页 > 运维架构 > Linux

Linux下查看程序内存占用

2017-01-15 22:30 309 查看

1

使用ps命令查看内存是不准确的,因为其返回的是一个进程所用的所有空间,而由于linux的共享内存机制,一个资源可能并非一个程序所独占。[详见]

结果显示中常出现的RSS和VSZ的区别。

- RSS:Resident Set Size 进程在RAM中占用的空间,不包括swap中的部分,包含共享内存中实际装载的部分,包含所有的栈和堆空间。单位KB

- VSZ : Virtual Memory Size 包括了进程所有可使用的空间,单位KB

举例说明:某程序有500K的binary,实际装载200K,链接到了2500K的共享库文件,实际装载了1000K,有200K的栈和堆占用,其中100K在内存中,剩余的是swapped。

RSS: 400K+1000K+100K

VSZ : 500K+2500K+200K

[详见]

2

使用pmap命令。

Usage:
pmap [options] PID [PID ...]

Options:
-x, --extended              show details
-X                          show even more details
WARNING: format changes according to /proc/PID/smaps
-XX                         show everything the kernel provides
-c, --read-rc               read the default rc
-C, --read-rc-from=<file>   read the rc from file
-n, --create-rc             create new default rc
-N, --create-rc-to=<file>   create new rc to file
NOTE: pid arguments are not allowed with -n, -N
-d, --device                show the device format
-q, --quiet                 do not display header and footer
-p, --show-path             show path in the mapping
-A, --range=<low>[,<high>]  limit results to the given range

-h, --help     display this help and exit
-V, --version  output version information and exit


如下为cmake的占用内存

mapped: 670196K    writeable/private: 80988K    shared: 9368K


3

使用cat /proc/PID/statm

size (1) total program size (same as VmSize in /proc/[pid]/status)

resident (2) resident set size (same as VmRSS in /proc/[pid]/status)

shared (3) number of resident shared pages (i.e., backed by a file) (same as RssFile+RssShmem in /proc/[pid]/status)

text (4) text (code)

lib (5) library (unused since Linux 2.6; always 0)

data (6) data + stack

dt (7) dirty pages (unused since Linux 2.6; always 0)

例子 693 406 586 158 0 535 0


[详见]

4

使用valgrind工具,生成程序运行时的快照

5

使用top

top -p <PID>


6

使用smem命令

有USS和PSS两种,USS是独占的内存,PSS还包含了按比例分得的共享内存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 内存