Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Task stack usage optimization

Posted by rrrum on March 3, 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.


Task stack usage optimization

Posted by edwards3 on March 3, 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, ...

Task stack usage optimization

Posted by rrrum on March 4, 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.

Task stack usage optimization

Posted by edwards3 on March 4, 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.

Task stack usage optimization

Posted by richard_damon on March 5, 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.

Task stack usage optimization

Posted by rrrum on March 8, 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.

Task stack usage optimization

Posted by xz8987f on March 8, 2016

I'm using this approach with GNU gcc which works well with FreeRTOS: http://mcuoneclipse.com/2015/08/21/gnu-static-stack-usage-analysis/


Task stack usage optimization

Posted by rtel on March 8, 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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Version 10.1.0 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner