Understanding Memory Allocation in FreeRTOS

Author | strongerHuang

Some readers might have asked this question: Can FreeRTOS create tasks and delete tasks, is it dynamically allocated memory?
xTaskCreate(LED_Task, "LED_Task", 128, NULL, 6, NULL);

This question involves many knowledge points, so let’s pick a few important related contents to answer this question.

Static and Dynamic Memory Allocation

Memory allocation generally divides into: static and dynamic allocation.

1. Static Memory Allocation

What is static memory allocation?

In simple terms, it is the memory allocated at compile time. You can understand it as when the chip is powered on, a specified area (address) of memory is occupied.

There are several situations, for example:

Startup allocation stack:

Stack_Size    EQU     0x400

Defining a static variable, this is the easiest example to understand:

static int a;

Defining a global variable/array, etc:

int char;

2. Dynamic Memory Allocation

What is dynamic memory allocation?
In contrast to static allocation, dynamic means the memory is allocated at an uncertain moment.
The most common case is when a function defines a local variable; if this function is called, a temporary memory space is allocated for this variable, and after the function execution, this memory is released.
void UART_Send(char *p){  char buf[10];  //......}
Another classic example is the malloc() dynamic memory allocation function, which is generally not recommended for ordinary developers due to some “disadvantages”.
For example: forgetting to use free() to release memory, and it is easy to produce “memory fragmentation”, etc.

Sorting Out FreeRTOS Memory Allocation

1. Global Array (Heap)

The memory of FreeRTOS is allocated in the FreeRTOSConfig.h file as a global array, and the size of this memory is allocated by the user according to the situation, for example:

#define configTOTAL_HEAP_SIZE     ((size_t)(10 * 1024))

Understanding Memory Allocation in FreeRTOS

Array (Heap) ucHeap:

Understanding Memory Allocation in FreeRTOS

This array is the “heap” of the FreeRTOS system, and creating tasks, semaphores, queues, etc., will call this heap.

2. Creating Tasks

FreeRTOS creates tasks and allocates stack size, for example: 128 “words”

xTaskCreate(LED_Task, "LED_Task", 128, NULL, 6, NULL);

Reminder:Here, “word” is a unit, for example: uint32_t

By further tracing the code, you will find that the “create task” function calls the [pvPortMalloc] function to allocate memory.

Understanding Memory Allocation in FreeRTOS

This is a user-defined function, not the standard malloc function.
It seems to be “dynamically allocating memory”, but in fact, it is “dividing” the globally defined array (heap) above.
For specific details on how it is “divided”, you can refer to the “heap_4.c” source code (by using the “heap_4.c” memory allocation method, if others can refer to the corresponding source code).
Other creations of semaphores, queues are also similar principles.

3. Deleting Tasks

FreeRTOS deletes tasks and calls the “vPortFree()” function to release the corresponding memory.

Understanding Memory Allocation in FreeRTOS

This involves TCB (Task Control Block), which is the data related to the task. (Not detailed here, we will talk about it later if there is a chance).

Is FreeRTOS dynamically allocated memory?

Having seen this, can you answer the initial question?

The answer is: FreeRTOS is not dynamically allocated memory, it just simulates the way of dynamic allocation, and the actual memory is statically allocated.

———— END ————

Understanding Memory Allocation in FreeRTOS

Leave a Comment

×