HEAP utilization

anuradha1 wrote on Thursday, June 16, 2011:


Can anyone explain the relationship among the following three.
                  1. configTOTAL_HEAP_SIZE defined in FreeRTOSConfig.h

                  2. STACK_SIZE  given when creating a task using xTaskCreate( vTaskCode, “NAME”, STACK_SIZE,     &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );

                   3.  Min Free Stack value given by the FreeRTOS state viewer Tasks during debugging with IAR

I am using STM32L

Thank you.

rtel wrote on Thursday, June 16, 2011:

See http://www.freertos.org/a00111.html for configTOTAL_HEAP_SIZE and how task stacks use it.  Also, but less useful http://www.freertos.org/a00110.html

http://www.freertos.org/a00125.html gives the prototype for the xTaskCreate() function, and says how stack sizes are specified.  The stack is allocated from the heap as per the first link above.

The minimum free stack size is I think explained in the application note that comes with the IAR plug-in, but is the same as described here: http://www.freertos.org/uxTaskGetStackHighWaterMark.html


anuradha1 wrote on Thursday, June 16, 2011:

Thanks for your immediate reply Richard.

Still having problems and needs clarifications.

My RAM is 10KBytes.
From that 1KB has to be reserved for the CSTACK.

13 tasks are created in my application. Total of 3328 bytes are allocated for the stack size of those tasks during xTaskCreate() function calls. Each task was given different stack sizes so that they will not have excess memory. (All having less than 100 minimum free stack each)

According to the map file the aggregation of RAM of all other files are around 1Kbytes.

Total of  all above is (1K + 3.3K + 1K) 5.3K. However we cannot run the system with configTOTAL_HEAP_SIZE less than 8Kbytes.

Please explain what happens to the (8K - 5.3K) RAM, which is not used by my files/functions.

Note that I am using heap_2.c


davedoors wrote on Thursday, June 16, 2011:

Do you really need the CSTACK once the tasks have started? CSTACK is normally used by main(), but after the scheduler has started each tasks have their own stacks and CSTACK is not required any more. You can then recycle CSTACK for other uses, and normally find its start address from the variables defined in the linker script. If that is the case or not depends on the port and microcontroller you are using. Some ports reuse CSTACK as an interrupt stack.

davedoors wrote on Thursday, June 16, 2011:

I was a bit hot on the add reply button there…

You cannot magic more RAM into a microcontroller, but you can select one from the same family that has more unless minimum recurring production code is really vital. However, another thing to consider is if you can cut down the number of tasks you have running. If some of the tasks perform periodic or run to completion functionality then you could implement them as timer callback functions instead of a task. All timer callbacks use the same stack as the timer task. If you can implement two tasks as timer callback functions, but add the timer task, overall you have still saved one entire stack of RAM.