Analyzing heap memory usage with libc allocator API

Updated: October 26, 2022

The libc library contains a data structure that you can read to retrieve statistics about the current heap memory usage. These statistics are more detailed than those reported by the System Information and can be read at precise code locations.

The _malloc_stats structure contains fields that report the used, overhead, and free heap space for both big and small blocks. For instance, the m_allocmem field stores the space in use by the program for big blocks. Here, “space in use” means how much memory was requested and doesn't include memory for storing block metadata. Likewise, the m_small_allocmem field stores the space in use for small blocks, which are blocks stored in bands preallocated by libc to improve performance.

Other useful fields include but aren't limited to:
You can attach the debugger and read these variables, which doesn't require modifying an application's code and recompiling it. However, you may prefer to write code that queries and prints heap statistics, to avoid stopping and restarting the program in the debugger (but this strategy requires recompiling). For instance, to print the total heap memory requested by the program, you would use code like this:
#include <malloc.h>
extern struct malloc_stats _malloc_stats;
printf("Used memory: %d\n", (_malloc_stats.m_allocmem + _malloc_stats.m_small_allocmem));
Note: You could output the heap statistics to a file or standard error instead of standard output; the above code is just for demonstrating how to read and print the data fields filled in by libc.

These statistics give you insight into the overhead that the memory allocator is generating and how much memory is being handed out to your application (and thus, shows up in a pidin memory listing as heap memory) but may not be used directly. In this SDP release, the _malloc_stats structure is defined in QNX_TARGET/usr/include/malloc.h. A short, descriptive comment is provided next to each field.