21. Heap Memory Usage

SDK6 uses four heap areas for dynamic memory allocation:
  • KE_MEM_ENV: Used for environment variables.

  • KE_MEM_ATT_DB: Used for service attribute databases.

  • KE_MEM_KE_MSG: Used for kernel messages.

  • KE_MEM_NON_RETENTION: Used for general purpose heap memory.

The SDK6 contains default values for the maximum size of each of these heap areas and normally no adjustment is necessary.

21.1. Heap Memory Resizing

The default values for the maximum heap sizes can be overridden using the following macros, defined in the file ..._config_advanced.h (considering the target name):

/***********************************************************************/
/* Custom heap sizes                                                   */
/***********************************************************************/
// #define DB_HEAP_SZ              1024
// #define ENV_HEAP_SZ             4928
// #define MSG_HEAP_SZ             6880
// #define NON_RET_HEAP_SZ         2048

Simply uncomment the macro(s) corresponding to the heap size you want to change, and then set the value accordingly. For example, if you want to increase the maximum size of the non retained heap to 3000 bytes, you would change the definition to the following:

/***********************************************************************/
/* Custom heap sizes                                                   */
/***********************************************************************/
// #define DB_HEAP_SZ              1024
// #define ENV_HEAP_SZ             4928
// #define MSG_HEAP_SZ             6880
- #define NON_RET_HEAP_SZ          2048
+ #define NON_RET_HEAP_SZ          3000

When an error occurs, logging the heap usage only tells you that you have run out of memory. It does not tell you how much bigger the heap area needs to be. Determining this is an itterative process; increase the relevant custom heap size macro, rebuild the application and then log the heap memory usage once again. After a couple of iterations you should have discovered a suitable maximum heap size.

Error

It might be tempting to simply increase the maximum heap size to a large value. We recommend against doing this. Using large memory areas for the heap reduces the amount of memory available for code and data storage.

Optimizing Heap Memory Usage

Defining a large number of services/characteristics can result in database heap exhaustion. It is recommended to keep the number of services and characteritics to a minimum.

Heap Memory Logging

In Displaying Heap Log we can find the steps to heap log information.