Quality RTOS & Embedded Software

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


Reducing Stack size

Posted by paulgigliotti on March 5, 2017

I am aware of tools for seeing how much stack is used by task. I am looking for some general guidelines on methods to reduce stack requirements. I am less concerned with the stack size, than by the time it takes to push data on to the stack upon a context switch. I have several tasks that wake up on a QueueRecieve. In these tasks, I have declared a buffer, into which the QueueReceive copies the data. The task processes the data, at at the end of the loop, returns to the QueueReceive, where it will become blocked, and wait for the next packet. What I am wondering is will the contents of the buffer (just processed) be pushed on to the stack. I no longer need this data, and moving it to the stack would be a waste of processor band width. If the data does not get stored, then fine. If not, would it be more advantages to create the buffer outside the process, and pass the address of the buffer into the task as a paramater. Would this then prevent the contents of the buffer from being pushed onto the stack?

Reducing Stack size

Posted by richarddamon on March 5, 2017

The time to do a context switch is NOT dependent on the amount of data that is on the stack. Each task has its own stack space, that remains were it is, and the context switch basically saves the register set, saves the value of the Stack Pointer into the tasls TCB, and then loads the stack pointer for the new task, and restores the registar set and resumes execution.

Reducing Stack size

Posted by paulgigliotti on May 11, 2017

Thanks for that. Ok, independant of context switch, what are things to consider to reduce stack size. We are seeing some stack overflow issues that we are chasing down and looking for ways to reduce the amount of stack space required.

Reducing Stack size

Posted by rtel on May 12, 2017

The stack usage is really nothing to do with the RTOS.

If you take a non-RTOS program and execute it then the amount of stack it uses is dependent on the function call depth, the compiler used, the compiler optimisation, and the local variables you declare. If you then take the same program and run it under the RTOS the stack usage will be very approximately 70 words larger. The small number of additional words are used to hold the task's context when the task is not running.

If you are using GCC then, with the right command line option (which I forget but are easy to look up), you will get a file that tells you the stack usage of every function as part of the build output.

You can also use uxTaskGetStackHighWaterMark() to query stack usage.

[ 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