Linux下查看程序内存占用

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还包含了按比例分得的共享内存。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值