Task stack usage optimization

rrrum wrote on Thursday, March 03, 2016:


I have aplication with about 15 tasks. Debugger with FreeRTOS plugin says that I have from 200-700 bytes free on each task. What is the rule for optimization for task stack in general? How much can I reduce free space? How can I be sure it is enough? In application with many tasks it is possible to achieve significant ram saving if the right approach is adopted.


edwards3 wrote on Thursday, March 03, 2016:

There is no point giving a task any more stack bytes that it is going to use because it would just be dead RAM, so if you need to optimize every byte of RAM you need to now how much stack each task uses. Like in any C program how many stack bytes are used depends on function call nesting depth, compiler optimization, how variables are declared, …

rrrum wrote on Friday, March 04, 2016:

Thank you for answer but it is very general and does not help me much. I know that there is a dead RAM if stacks are oversized thats why I deal this problem. It is very difficult calculate function nesting in enviroment with OS. I also cannot just reduce on limit and then wait on stack overflow. So I was asking if anybody has a simple rule to be safe but on the other hand don’t waist ram.

edwards3 wrote on Friday, March 04, 2016:

There is practically no difference in the stack used with and without FreeRTOS if both are compiled with the same compiler.

Without FreeRTOS there is the stack used by main() but no stack used by the task context.

In a FreeRTOS task main() has no influence, but you need about 20 words for the context.

richard_damon wrote on Saturday, March 05, 2016:

Rum, difinitively figuirng out how much stack a general program/task will take is a ‘hard’ task, and often the best choise is to run it (over a variety of input conditions) and measure it, and sometimes add a safety factor in case you missed a path.

If the program doesn’t use recursion, than some compilers/linkers will have an option to compute the worse case stack usage of a function if it meets some basic limits. You can also do this by hand, but you may need a bit of help from the compiler.

You can estimate the stack needed by a function by adding up the size of all the local variables and parameters it uses, adding a bit for temporaries needed, and call frames, and then adding what is needed by the things it calls (and the things they call and so on). You may have to guess on the requirements for library functions, but this is normally not the major usage.

rrrum wrote on Tuesday, March 08, 2016:

Thanks Richard, What kind of help I can get from GCC? What could be the safety factor value? Are there any tools/debuggers/profilers etc… available specifically for FreeRTOS to help with this task? Are they effective? In the past I could see such tool from Infineon. Since the stack is dedicated to each task individually its sizing is very critical because overflow usually causes system crash.

xz8987f wrote on Tuesday, March 08, 2016:

I’m using this approach with GNU gcc which works well with FreeRTOS:


rtel wrote on Tuesday, March 08, 2016:

Sorry for the brevity of my reply. The FreeRTOS+Trace tool will trace memory allocations. See the FreeRTOS stack FAQ for a link to Erich’s blog post on getting stack usage info from GCC.