Skip to content
Advertisement

How can i get memory usage by a process in freeRTOS

As we all know, we can get RAM currently used by a process in Linux using commands like ps, top and vmstat or reading the pseudo-filesystem /proc. But how can i get the same information in freeRTOS where we could not use commands and there exist no file system.

Advertisement

Answer

First there’s no process context in RTOS. In FreeRTOS there’re tasks(which are analogous to threads in Linux) and the main context which again is lost once the Scheduler is started. The stack memory occupied by each task is configured by the client at task creation.

However once the system is running you can query if the stack reaches its maximum value by using the following API.

uxTaskGetStackHighWaterMardk(TaskHandle_t task)

Please refer https://www.freertos.org/uxTaskGetStackHighWaterMark.html

Remember that INCLUDE_uxTaskGetStackHighWaterMark should be defined to 1 to use this feature.

For heap memory I assume you’re using one of FreeRTOS heap allocation strategies(heap_1,heap_2 etc). In that case if you’ve globally overridden your malloc/free/new/new[]/delete/delete[] to use FreeRTOS pvPortMalloc, there’s a way to register a hook function that gets called when the system runs out of heap.

Refer https://www.freertos.org/a00016.html

At the same time it is possible to retrieve run time status from the scheduler by using the following API.

void vTaskGetRunTimeStats( char *pcWriteBuffer );

Of course, this will suspend/unsuspend the scheduler frequently, so will not be a real solution for your production code, but is still a good debugging aid.

Refer https://www.freertos.org/rtos-run-time-stats.html.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement