Heap Memory profiling in C++

09/10/2023

A quick guide on how to easily profile heap memory usage and investigate memory issues in C++

This is a quick guide on how to setup and use memory profiling utilities for C++ programs using valgrind, massif, and massif-visualizer in Ubuntu, to quickly identify memory issues.

Install valgind

sudo apt install valgrind

Install massif-visualizer

sudo snap install massif-visualizer

Or, if you don’t like snaps:

add-apt-repository ppa:kubuntu-ppa/backports && apt-get update && apt-get install massif-visualizer

Then, the following commands should be available in the console:

➜  ~ valgrind --version    
valgrind-3.18.1
➜  ~ massif-visualizer --version
massif-visualizer 0.7

Compile the program you want to investigate with debug symbols (Debug configuration)

Then execute valgrind with massif:

valgrind --tool=massif programName

The program should start. Once it exits (or you quit the program), a file with a name similar to this: massif.out.1282627 should get created in the directory where you launched valgrind from.

Then you can execute massif-visualizer and open the file that just got created.

All posts
davespace.xyz – 2024