Fundamentals 8 min read

FreeRTOS Memory Allocation Methods, Interfaces, and Management Schemes

FreeRTOS supports both static and dynamic memory allocation, allowing tasks, timers, semaphores and mutexes to use either a pre‑allocated region or the system heap, provides pvPortMalloc/vPortFree APIs, and offers five heap schemes (heap_1‑5) ranging from simple non‑freeing allocation to multi‑region coalescing management for flexible embedded applications.

OPPO Kernel Craftsman
OPPO Kernel Craftsman
OPPO Kernel Craftsman
FreeRTOS Memory Allocation Methods, Interfaces, and Management Schemes

FreeRTOS provides two primary memory allocation approaches: static allocation and dynamic allocation. Before version V9.0.0, system resources such as tasks, software timers, semaphores, and mutexes could only obtain memory from the configured heap. Starting with V9.0.0, these resources can also be allocated from a user‑defined memory region, allowing custom memory placement.

Static allocation assigns memory for tasks, timers, semaphores, and mutexes from a pre‑allocated region (e.g., a global array) without invoking the pvPortMalloc interface. The memory layout is fixed and shown in the accompanying diagram.

Dynamic allocation uses the pvPortMalloc interface to request memory from the system heap (ucHeap). Except for the heap_3.c scheme, which directly calls the C library malloc, other heap implementations manage memory according to their specific algorithms. The resulting memory distribution is illustrated in a second diagram.

FreeRTOS also defines several memory‑operation APIs:

pvPortMalloc : Allocate memory using the selected heap implementation.

vPortFree : Release previously allocated memory.

vPortInitialiseBlocks : Initialise the heap structures.

xPortGetFreeHeapSize : Query the current amount of free heap memory.

xPortGetMinimumEverFreeHeapSize : Query the minimum ever free heap size recorded.

The kernel supplies five heap implementations located in Source/portable/MemMang :

heap_1.c : Only allocation is allowed; memory is never freed. Suitable for safety‑critical embedded systems with deterministic memory usage.

heap_2.c : Uses a best‑fit algorithm with a linked‑list of free blocks. Memory can be freed, but adjacent free blocks are not coalesced, leading to fragmentation.

heap_3.c : Wraps the standard C malloc / free functions with thread‑safety. Allocation and deallocation are nondeterministic.

heap_4.c : Similar to heap_2.c but adds a coalescing step that merges adjacent free blocks, reducing fragmentation and improving efficiency.

heap_5.c : Extends heap_4.c by supporting multiple non‑contiguous memory regions (e.g., internal RAM and external SRAM) defined via the HeapRegion_t structure.

Each heap implementation is accompanied by source code excerpts and diagrams that illustrate its allocation strategy and behaviour.

In summary, FreeRTOS offers flexible memory allocation mechanisms and five distinct heap management schemes, enabling developers to choose the most appropriate strategy for their embedded application requirements.

Memory Managementdynamic allocationEmbedded SystemsFreeRTOSStatic Allocation
OPPO Kernel Craftsman
Written by

OPPO Kernel Craftsman

Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.