Collecting heap usage statistics

Technical Note 28545

Architectures:

ARM

Component:

general

Updated:

9/22/2017 10:23 AM

Introduction

The heap is described in the IAR C/C++ Development Guide for ARM, under chapter Dynamic memory on the heap. This Technical Note describes how to collect heap usage statistics in an application. That is, how to track the amount of memory used by malloc and similar functions.

Discussion

With IAR Embedded Workbench for ARM version 6.60 and later, you can collect heap usage statistics by using the function call:

__iar_dlmallinfo()

If you want a simple printout on stderr, call the function:

__iar_dlmalloc_stats()

The output from __iar_dlmalloc_stats() looks like (for example):

max system bytes =     2048
system bytes     =     2048
in use bytes     =       16

The functions above are declared and defined in the files arm\src\lib\dlib\heap\dlmalloc.c and arm\src\lib\dlib\heap\dlmalloc_stat.c. For IAR Embedded Workbench for ARM versions earlier than 8.10, these files can be found in the arm\src\lib folder instead.

To be able to call these functions, use the header file mallocstats.h that you can download here. The zip file also contains an example file main_test.c that shows how to use the function calls.

Legacy versions

For IAR Embedded Workbench for ARM versions earlier than version 6.60, you must include the file arm\src\lib\dlmalloc.c in your project, and set the defines NO_MALLINFO and NO_MALLOC_STATS to 0. Note that you must copy the dlmalloc.c file to the project directory and modify it there. Also note that in a C++ project, the dlmalloc.c file must be compiled as a C file.

Maximum heap usage

The following text is from the article Mastering stack and heap for system reliability, under the heading How to set the heap size.

"For systems that mimic desktop systems by emulating sbrk(), the maximum heap usage will be given by malloc_max_footprint(). For embedded systems that do not emulate sbrk(), it is common to give the entire heap to the memory allocator in one chunk. In that case malloc_max_footprint() becomes worthless; it will simply return the size of the entire heap."

IAR Embedded Workbench does not emulate sbrk().

There are some tips and tricks in the article on how to calculate how much heap is used.

Conclusion

This Technical Note describes how to collect heap usage statistics from an application.

 

All product names are trademarks or registered trademarks of their respective owners.

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.