Monitoring Memory Usage in FreeRTOS

Hello community

I’m trying to understand how memory is being used in my FreeRTOS project specifically the usage of .text, .data, .bss, stack, and heap. In Linux systems, I’ve seen it’s possible to monitor memory utilization quite clearly, but I don’t know how to do the same in an FreeRTOS environment. Could you please explain how to check memory usage for each of these segments in a real project? Also, how can I ensure I’m not running into stack overflows or heap exhaustion during runtime?

For reference, I’m using ESP32 and Arduino IDE but feel free to use your own hardware/software setup if that helps explain things more effectively

.text, .data, and .bss segments are pretty much just like any environment, look at you map file.

for the heap, it depends which version of heap*.c you are using as far as what portMalloc will take from, and/or how you made the regular malloc thread safe (if you did). The heap routines have functions that let you ask how much of the memory has been used to monitor. You can enable the malloc failure hook to get notification that the heap has been exhausted (at least for calls to portMalloc).

Stacks are the bigger issue, first, they are allocated out of either the heap, or .bss (or possibly .data) depending on how you create the tasks. As far as checking the stack usage of tasks themselves, one options is to enable stack overflow checking with configCHECK_FOR_STACK_OVERFLOW which checks every time a task is switched out. A value of 1 checks the stack pointer, which is quick, a value of 2 looks to see if the last few words in the stack have been written to, which costs a bit more time.

There is also the function xTaskGetInfo that cn give you the stack usage of a task, or the uxTaskGetSystemState function that will get the information for all the tasks in the system.

2 Likes