massif profiles heap memory usage over time, showing how much memory is allocated at each point in the program. Use it to find memory leaks, optimize memory footprint, and identify allocation hotspots.
valgrind --tool=massif ./program ms_print massif.out.12345
massif samples the heap periodically (every ~10000 allocations by default) and records the size. After the program finishes, ms_print visualizes the memory profile over time as an ASCII graph.
Output:
MB
20 | #
| ## #
15 | ## # #
| ## # # #
10 | ## # # # #
| ## # # # # #
5 | ######## ## # # # # # #
| # # ## # # # # # # #
0 |__# #_____________________#________________
0 1e5 2e5 3e5 Time (allocations)
The graph shows heap size (MB) vs. time (allocation count). Each # represents a sampling point. Spikes indicate temporary allocations; sustained levels indicate long-lived data.
Peak memory: massif reports the peak heap size reached during execution. This is useful for understanding worst-case memory usage and checking if optimizations reduce peak memory.
Detailed analysis:
ms_print massif.out.12345 | less
The full report includes a timeline of snapshots. Each snapshot shows the largest allocations at that point, with their allocation stack traces. This reveals which code path allocated the most memory at peak.
Example snapshot:
Heap at 10,000 allocations: n time(B) total(B) useful-heap(B) extra(B) count 1 1,234,567 10,000,000 9,500,000 500,000 1000 2 500,000 5,000,000 4,750,000 250,000 500 3 100,000 1,000,000 900,000 100,000 100 Detailed profiling with stack traces...
“useful-heap” is what the program requested; “extra” is Valgrind's memory overhead (typically 5-10% for malloc/free tracking).
Leak detection: Compare heap size at program start vs. exit. Large sustained allocations that persist until exit may be leaks. Use Valgrind memcheck for precise leak detection; use massif for understanding overall memory usage patterns.
Memory profiling on large programs:
valgrind --tool=massif --pages-as-heap=yes ./program # profile all memory, not just heap
By default, massif only tracks heap allocations (malloc, new). --pages-as-heap profiles all memory pages, including stack and mmap allocations. Useful for understanding total memory usage.
Usage: Run massif on a subset of your program (a single test case or representative workload) to understand memory patterns. For large HPC runs, extract a small problem size and profile that.