How can I measure the actual memory usage of an application or process? How can I measure the actual memory usage of an application or process? linux linux

How can I measure the actual memory usage of an application or process?


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 (e.g. 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.


Try the pmap command:

sudo pmap -x <process pid>


It is 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 statistics from the /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 statusName:      firefoxState:     S (sleeping)Tgid:      19420Pid:       19420PPid:      1TracerPid: 0Uid:       1000    1000    1000    1000Gid:       1000    1000    1000    1000FDSize:    256Groups:    4 6 20 24 25 29 30 44 46 107 109 115 124 1000VmPeak:    222956 kBVmSize:    212520 kBVmLck:          0 kBVmHWM:     127912 kBVmRSS:     118768 kBVmData:    170180 kBVmStk:        228 kBVmExe:         28 kBVmLib:      35424 kBVmPTE:        184 kBThreads:   8SigQ:      0/16382SigPnd:    0000000000000000ShdPnd:    0000000000000000SigBlk:    0000000000000000SigIgn:    0000000020001000SigCgt:    000000018000442fCapInh:    0000000000000000CapPrm:    0000000000000000CapEff:    0000000000000000Cpus_allowed:    03Mems_allowed:    1voluntary_ctxt_switches:    63422nonvoluntary_ctxt_switches: 7171