Analyzing heap memory usage with Valgrind Massif

Updated: May 06, 2022

Valgrind Massif analyzes a program's heap usage by taking heap snapshots as the program runs, then outputs the snapshot data when the program terminates. The IDE visually presents the data by graphing the heap usage over time.

Note: All Valgrind tools can be loaded and run from the command line. However, using the IDE is more convenient because it automates much of the setup by setting Valgrind command options based on UI fields and by copying the analysis results into the host workspace.

If your target image isn't configured to include the valgrind binaries and libraries, the IDE attempts to upload these components when you launch an application with a Valgrind tool enabled. For this to work, the target must have a writable filesystem.

To analyze heap usage with Valgrind Massif:
  1. In the launch bar, expand the Launch Configuration dropdown (which is in the middle) and select the project that you want to analyze.
  2. In the Launch Target dropdown (on the right), select the target for running your application.
  3. In the Launch Mode dropdown (on the left), select Memory.
  4. Click the Edit button (Icon: Edit button) on the right of the Launch Configuration dropdown.
  5. In the configuration editor window, access the Valgrind controls by clicking the Memory tab on the right and then the Valgrind radio button near the top of this tab.
  6. Select Massif from the Tool to run dropdown.
  7. Optional: You can change any settings to customize what gets reported in the Valgrind results.
    The Massif tab lets you specify how many heap snapshots are taken and how the captured data are presented.
  8. Click OK to save the configuration changes and close the window.
  9. In the launch bar, click the Memory button (Icon: Memory button).

The IDE switches to the QNX Analysis perspective. If necessary, the IDE first builds the binary before uploading it to the target. To analyze the application, the IDE instructs Valgrind to execute the uploaded binary with Massif instrumentation. Then, it creates a session for storing the Valgrind results; this new session is displayed in the Analysis Sessions view. When the program terminates, Valgrind writes the results to a log file, which the IDE copies into the directory for the new session.

The memory measurements taken at each heap snapshot are graphed and displayed in a new editor window called Heap Chart. The chart drawn is a line graph that illustrates how the application's heap usage changed over time, with diamond-shape points indicating individual snapshots:

Screenshot of Heap Chart window containing a line graph illustrating changes in heap usage for a sample application

The chart plots the heap size along the vertical axis and the execution progress along the horizontal axis. For the execution progress, the metric used is determined by the Time unit field.

If you click a point in the chart, the corresponding snapshot is highlighted in the Valgrind view. This view displays the heap snapshot data in a table. Each row describes a single snapshot, listing the time measurement and the program's total, useful, and extra heap space at that moment:

Screenshot of Valgrind view containing the heap snapshot data from a Massif session, displayed in a table

Some heap snapshots are detailed, meaning they contain information about where the current blocks were allocated. The rows for these snapshots are indicated with the heap tree icon (Icon: Heap tree). Double-clicking in one of these rows displays a tree-like listing of allocation sources, as illustrated and explained in Finding unused memory with Valgrind Massif.

Note: You can run multiple Valgrind sessions concurrently, using the same tool or different tools, on the same application or different applications. Valgrind log files always contain the PIDs of the Valgrind processes, so their names are always distinct.