valgrind --tool=massif: Heap Profiling
valgrind --tool=massif records how a program uses the heap over time and which call sites allocate the most.
When peak memory is the problem, Massif shows not just the total but where and when it accumulates. Its output feeds ms_print and massif-visualizer.
What it does
Massif takes periodic snapshots of heap usage during a run and records the allocation call stacks responsible. The result is a massif.out file describing heap growth over time and the top allocators at peak.
Common usage
valgrind --tool=massif ./my-program
# also account for stacks and pages
valgrind --tool=massif --stacks=yes ./my-program
# name the output file
valgrind --tool=massif --massif-out-file=massif.out ./my-programOptions
| Flag | What it does |
|---|---|
| --tool=massif | Select the Massif heap profiler |
| --massif-out-file=<f> | Path for the output snapshot file |
| --stacks=yes | Also profile stack memory (slower) |
| --pages-as-heap=yes | Profile all memory at page granularity |
| --detailed-freq=N | Take a detailed snapshot every N snapshots |
In CI
Archive massif.out as a build artifact, then render it locally with ms_print or massif-visualizer. Like all valgrind tools it runs many times slower than native, so give the step a longer timeout.
Common errors in CI
An empty or truncated massif.out means the program crashed or was killed (OOM or timeout) before Massif wrote its final snapshot. "Massif: ... out of memory" on huge heaps means the profiler itself exhausted memory; reduce the workload. As with Memcheck, timeouts from the slowdown are the usual CI failure.