IAR DLIB Library heap usage statistics

Technical Note 28545



8/24/2012 1:40 PM


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


Download and open the Example project.


With IAR Embedded Workbench for ARM 6.10 and later, there is a possibility to include the file "arm\src\lib\dlmalloc.c" in your project, and set the define NO_MALLINFO to 0. (Note that you have to copy the dlmalloc.c file to the project directory and modify it).

By doing the above you can get heap usage statistics using the function call:


If you want a simple printout on stderr, the define NO_MALLOC_STATS needs to be set to 0. Then call the function:


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

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

Note: The example project can also be used in a C++ environment (for the "new" operator), but note that the "dlmalloc.c" file needs to be compiled as C language.

Workaround for IAR Embedded Workbench for ARM version 6.30.1

The example project issues a linker error with v6.30.1

Error[Li009]: runtime model conflict

The workaround is to set "Project > Options > Linker > Extra Options":

--redirect malloc=__iar_dlmalloc
--redirect free=__iar_dlfree

Maximum heap usage

The following extract is from the article Mastering stack and heap for system reliability, chapter 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.

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

Related Tech Notes

© IAR Systems 1995-2016 - All rights reserved.