How Much Memory to Allocate for FreeRTOS

Follow/Star Public Account, never miss a message!

How Much Memory to Allocate for FreeRTOS

A friend asked: Brother Huang, I just learned FreeRTOS not long ago, I want to ask, how much stack should I allocate when creating a task?

1Introduction

This question about memory allocation in the FreeRTOS operating system can be clarified if you have a general understanding of memory management and resource allocation.

If analyzed in depth, there is a lot of knowledge about operating system memory allocation, but this article will only discuss some relevant knowledge related to the initial question.

2

Operating System Memory Management

FreeRTOS provides various memory management methods, with 5 files in the MemMang folder: heap_1 to heap_5.

Heap actually means a heap, in other words, the memory of the FreeRTOS system is allocated through the heap implementation. (There is a lot of knowledge about memory management, which you can learn more about online when you have time, and I will not elaborate here)

Currently, the commonly used memory management method in FreeRTOS is heap_4: by pre-allocating a heap (array) for system resources, as shown in the figure below:

How Much Memory to Allocate for FreeRTOS

1. Configurable externally, or allocate the heap in heap_4;

2. Configure the size of the heap through FreeRTOSConfig.h, note that the size unit is bytes.

Tip: The other management methods from heap_1 to heap_5 are actually quite different, which will not be discussed here (I will elaborate on this later), you can refer to:

https://www.freertos.org/a00111.html

3

Memory Allocation for System Resources

In FreeRTOS, the system allocates memory for various resources, and using heap_4 memory management method means allocating a segment of memory at the beginning of creation.

The allocated resource is a portion taken from the entire system heap ucHeap. When we create a task, the principle of allocating stack size can be further examined in the code:

How Much Memory to Allocate for FreeRTOS

Tip: The size for creating a task is filled in as x4 bytes. For example, filling in 128 actually allocates 512Bytes. (Many people think the unit is bytes, but when they create several tasks, they find the memory allocation fails)

For example, creating a queue:

How Much Memory to Allocate for FreeRTOS

Parameter 1: Queue length, which is the number of queues;

Parameter 2: Queue size, which is how many bytes each queue has;

The memory size allocated for the queue is: Parameter 1 x Parameter 2 (unit Byte).

Here are just two examples, the key point is to emphasize: the unit for memory allocation is bytes (Byte), or words (4Byte).

4

Which Variables Occupy Task Memory

Returning to the initial question: how much memory should a task allocate?

The task stack is used to store the variable data of that task (that is, when the program is executing other tasks, it needs to save the variable data of that task).

So, what data counts as “variable data” for a task?

One is the memory space needed by the task itself, the FreeRTOS task itself (like TCB) requires about seventy or eighty bytes (I calculated this before, but I don’t remember very clearly).

The second is your own application temporary variables (as shown in the figure below), which is calculated or estimated by you.

How Much Memory to Allocate for FreeRTOS

Many friends directly allocate a stack size of 256 (x4 bytes) or 512 (x4 bytes), but their own application temporary variables are not many.

If your application only has a few bytes of temporary variables, allocating 64 (x4 bytes) is completely sufficient.

Tip: This “temporary variable” includes the variables allocated by the functions called by your application.

Well, that’s all I want to teach you, some knowledge is interrelated.

Recommended Reading:

1. About the position of local variable definitions within the function body

2. There is a modifier that can prevent a variable from being initialized during processor reset

5Conclusion

If you find the article helpful, liking and sharing is also a great support and encouragement for me.

Scan the QR code below, follow the public account, and check the bottom menu for more exciting content!

How Much Memory to Allocate for FreeRTOS

Long pressto recognize the QR code in the imageto follow

How Much Memory to Allocate for FreeRTOS

Appreciation is a recognition and support for the author!

Leave a Comment

×