Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

5.3.3. Profiling Heap and Stack Space with Massif

Massif measures the heap space used by a specified program; both the useful space, and any additional space allocated for book-keeping and alignment purposes. It can help you reduce the amount of memory used by your program, which can increase your program's speed, and reduce the likelihood that your program will exhaust the swap space of the machine on which it executes. Massif can also provide details about which parts of your program are responsible for allocating heap memory. Programs run with Massif run about twenty times more slowly than their normal execution speed.
To profile the heap usage of a program, specify massif as the Valgrind tool you wish to use:
# valgrind --tool=massif program
Profiling data gathered by Massif is written to a file, which by default is called massif.out.pid, where pid is the process ID of the specified program.
This profiling data can also be graphed with the ms_print command, like so:
# ms_print massif.out.pid
This produces a graph showing memory consumption over the program's execution, and detailed information about the sites responsible for allocation at various points in the program, including at the point of peak memory allocation.
Massif provides a number of command line options that can be used to direct the output of the tool. Some of the available options are:
--heap
Specifies whether to perform heap profiling. The default value is yes. Heap profiling can be disabled by setting this option to no.
--heap-admin
Specifies the number of bytes per block to use for administration when heap profiling is enabled. The default value is 8 bytes per block.
--stacks
Specifies whether to perform stack profiling. The default value is no (disabled). To enable stack profiling, set this option to yes, but be aware that doing so will greatly slow Massif. Also note that Massif assumes that the main stack has size zero at start-up in order to better indicate the size of the stack portion over which the program being profiled has control.
--time-unit
Specifies the unit of time used for the profiling. There are three valid values for this option: instructions executed (i), the default value, which is useful in most cases; real time (ms, in milliseconds), which can be useful in certain instances; and bytes allocated/deallocated on the heap and/or stack (B), which is useful for very short-run programs, and for testing purposes, because it is the most reproducible across different machines. This option is useful when graphing Massif output with ms_print.
For a full list of options, refer to the documentation included at /usr/share/doc/valgrind-version/valgrind_manual.pdf.