How to measure actual memory usage of an application or process?

This question is covered here in great detail.

How do you measure the memory usage of an application or process in Linux?

From the blog article of Understanding memory usage on Linux, ps is not an accurate tool to use for this intent.

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running . Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely wrong.


With ps or similar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:

  • does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it

  • can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries

  • If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of valgrind is called 'massif':

    Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

    As explained in the valgrind documentation, you need to run the program through valgrind:

    valgrind --tool=massif <executable> <arguments>
    

    Massif writes a dump of memory usage snapshots (eg massif.out.12345 ). These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print , a simple text-based tool shipped with valgrind, to be of great help already.

    To find memory leaks, use the (default) memcheck tool of valgrind.


    试试pmap命令:

    sudo pmap -x <process pid>
    

    Hard to tell for sure, but here are two "close" things that can help.

    $ ps aux 
    

    will give you Virtual Size (VSZ)

    You can also get detailed stats from /proc file-system by going to /proc/$pid/status

    The most important is the VmSize, which should be close to what ps aux gives.

    /proc/19420$ cat status
    Name:   firefox
    State:  S (sleeping)
    Tgid:   19420
    Pid:    19420
    PPid:   1
    TracerPid:  0
    Uid:    1000    1000    1000    1000
    Gid:    1000    1000    1000    1000
    FDSize: 256
    Groups: 4 6 20 24 25 29 30 44 46 107 109 115 124 1000 
    VmPeak:   222956 kB
    VmSize:   212520 kB
    VmLck:         0 kB
    VmHWM:    127912 kB
    VmRSS:    118768 kB
    VmData:   170180 kB
    VmStk:       228 kB
    VmExe:        28 kB
    VmLib:     35424 kB
    VmPTE:       184 kB
    Threads:    8
    SigQ:   0/16382
    SigPnd: 0000000000000000
    ShdPnd: 0000000000000000
    SigBlk: 0000000000000000
    SigIgn: 0000000020001000
    SigCgt: 000000018000442f
    CapInh: 0000000000000000
    CapPrm: 0000000000000000
    CapEff: 0000000000000000
    Cpus_allowed:   03
    Mems_allowed:   1
    voluntary_ctxt_switches:    63422
    nonvoluntary_ctxt_switches: 7171
    
    
    链接地址: http://www.djcxy.com/p/14066.html

    上一篇: 什么是专用字节,虚拟字节,工作集?

    下一篇: 如何测量应用程序或进程的实际内存使用情况?