The latest development version of this page may be more current than this released 0.4.3 version.

Heap Memory Management

Overview

The heap is a region of memory used for dynamic memory allocation during program execution.

Heap Memory

Difference Between Stack and Heap

The WM IoT SDK application uses a common computer architecture pattern: dynamically allocated memory by program control flow (stack), dynamically allocated memory by function calls (heap), and static memory allocated at compile time.

Since the WM IoT SDK operates in a multithreaded RTOS environment, each RTOS task has its own stack, which is allocated from the heap by default when the task is created.

In most cases, heap allocation can be achieved directly using the standard C library functions malloc() and free() . To make full use of the various types of memory and their characteristics, the WM IoT SDK also includes an attribute-based heap memory allocator.

To allocate memory with specific attributes, use:

wm_heap_caps_alloc()

Memory Attributes

The W800 includes various types of RAM:

  • SRAM

  • DRAM

  • PSRAM

To get the remaining space size of all RAM heaps, call:

wm_heap_get_free_heap_size()

When calling malloc(), the WM IoT SDK internally calls the wm_heap_caps_alloc function, using the attribute WM_HEAP_CAP_DEFAULT to allocate memory. Memory reallocation calls the wm_heap_caps_realloc function. Allocated memory can be freed by calling the wm_heap_caps_free function. To display heap memory statistics, call the wm_heap_print_stats function. For debugging, display heap memory allocation information by calling the wm_heap_print_tracing function. Currently, all memory allocated from the heap is 8-byte aligned.

RAM Usage