Analyzing heap memory usage with Memory Analysis

Updated: October 28, 2024

You can launch an application with Memory Analysis enabled to observe the heap activity over the application's lifetime. The Memory Analysis tool reads the data sent by the debug allocation library (librcheck) and displays graphs and event information describing heap allocations and deallocations.

Note: You can run an application that uses librcheck from the command line. However, using the IDE is more convenient because the Memory Analysis tool automates the requesting of data from this memory-tracing library and presents the results in a convenient format.
To analyze heap usage with Memory Analysis:
  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.
    Note: Although you can select Debug mode and manually enable Memory Analysis in the launch configuration, we recommend that you don't run this tool at the same time as the GDB debugger because the setup is more complicated. If you absolutely have to run both tools concurrently, you must follow the steps given in Running Memory Analysis and the GDB Debugger concurrently instead of the remaining steps given here.
  4. Optional: To confirm that Memory Analysis is the active tool or to adjust how it measures and reports heap usage, you must examine the launch configuration. To do this:
    1. Click the Edit button (Icon: Edit button) on the right of the Launch Configuration dropdown.
    2. In the configuration editor window, click the Memory tab on the right to access the tool controls.
    3. Ensure that the Memory Analysis radio button is selected. In the fields underneath, you can set the stack trace and heap snapshot settings based on what you want to see in the results.
    4. Click OK to save the configuration changes and close the window.
  5. In the launch bar, click the Memory button (Icon: Memory button).

The IDE switches to the QNX Analysis perspective. If necessary, it first builds the binary. Then, the IDE uploads the binary and starts running it on the target, with librcheck loaded. When this happens, a new session is created for storing the data produced by the librcheck library; this session is displayed in the Analysis Sessions view.

The IDE also opens an editor window that shows the program's heap memory activity. The Allocations tab is selected by default and it displays two charts that illustrate the allocation and deallocation sizes for all events and for a selected subset of events.

But the most important editor tab is the Usage tab, which displays two charts. Heap Usage Snapshot illustrates the percentages of used, free, and overhead heap memory in a piechart, while Heap Usage Trend shows the changes in size for these heap portions in a line graph:

Screenshot of both charts in the Usage tab, with a timeframe selected in the bottom chart and the corresponding heap usage data shown in the top chart

The other editor tabs display different data, such as the number of allocations within specific byte size ranges (or bins) and the usage of pre-allocated memory bands. All editor tabs are explained in full in How to use the Memory Analysis editor.

Note: You can run multiple Memory Analysis sessions concurrently, on the same application or different applications, but you must specify distinct file or device output paths for these sessions in the Advanced settings.
  翻译: