Tracking Actively Used Memory in Linux Programs

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.

Retrieve CPU usage and memory usage of a single process on Linux?

ps -p <pid> -o %cpu,%mem,cmd

(You can leave off "cmd" but that might be helpful in debugging).

Note that this gives average CPU usage of the process over the time it has been running.

Memory access monitor for c programs

If you're willing to monitor every memory access, I'd suggest to take a look at the packages such as PIN [1] and/or DynInst [2]. Both are dynamic binary instrumentation packages that allow you to modify an application binary to inject the code you want. In this case, both tools allow you to instrument every single instruction and know the address they reference (if they are loads/stores). Then, if you're only interested in memory allocated by malloc (or realloc, or calloc) you can also instrument these routines to capture their entry parameters and exit values to determine the region of memory of interest. Both tools provide similar functionalities. I'd say that their main difference is that PIN specifically focuses to Intel processors while DynInst is an open source project that supports different processor architectures (Intel, IBM-Power, ARM).

Since instrumentation may be costly in this particular scenario that instruments every instruction and you can afford to sample memory references, I'd suggest you to explore the PEBS infrastructure [3] from recent Intel processors (AMD processors have something similar named IBS). PEBS can be used from the perf tool available within the Linux OS [4] (I don't know if it is available on other OSeS)

[1] PIN https://software.intel.com/en-us/articles/pin-a-dynamic-binary-instrumentation-tool

[2] DynInst http://www.dyninst.org

[3] Intel Manual Section 18.4.4 Precise Event Based Sampling (PEBS) http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3b-part-2-manual.pdf

[4] Linux perf to sample memory addresses https://lwn.net/Articles/531766/



Related Topics



Leave a reply



Submit