0
0
FreeRTOSprogramming~3 mins

Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION) in FreeRTOS - When to Use Which

Choose your learning style9 modes available
The Big Idea

What if your device crashes because it ran out of memory at the worst moment?

The Scenario

Imagine you are building a small robot and you need to give it memory to store tasks. You guess how much memory it might need and set aside that much before turning it on.

But what if the robot needs more memory later or less? You either waste memory or run out, causing problems.

The Problem

Manually guessing memory size is slow and risky. If you allocate too little, your robot crashes. If you allocate too much, you waste precious memory.

Changing memory size later means rewriting code and retesting everything, which is tiring and error-prone.

The Solution

Static and dynamic allocation in FreeRTOS let you choose how to give memory to tasks.

Static allocation reserves fixed memory upfront, making your system stable and predictable.

Dynamic allocation lets your system grab memory when needed, saving space but needing careful management.

Before vs After
Before
void createTask() {
  char stack[100]; // fixed size
  // create task with fixed stack
}
After
StaticTask_t xTaskBuffer;
StackType_t xStack[100];
TaskHandle_t xHandle = xTaskCreateStatic(taskFunction, "Task", 100, NULL, 1, xStack, &xTaskBuffer);
What It Enables

You can build reliable embedded systems that use memory efficiently and avoid crashes.

Real Life Example

In a smart thermostat, static allocation ensures the temperature control task always has memory, preventing sudden failures during heating or cooling.

Key Takeaways

Static allocation reserves fixed memory for tasks before running.

Dynamic allocation assigns memory when tasks start, saving space but needing care.

Choosing the right method helps build stable and efficient embedded systems.