Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION) in FreeRTOS - Performance Comparison
When FreeRTOS creates tasks or queues, it can use static or dynamic memory allocation.
We want to see how the choice affects the time it takes to allocate memory as the system runs.
Analyze the time complexity of task creation using static and dynamic allocation.
// Dynamic allocation example
TaskHandle_t xTaskHandle = NULL;
xTaskCreate(
vTaskCode, "Task", 1000, NULL, 1, &xTaskHandle
);
// Static allocation example
static StaticTask_t xTaskBuffer;
static StackType_t xStack[1000];
xTaskCreateStatic(
vTaskCode, "Task", 1000, NULL, 1, xStack, &xTaskBuffer
);
This code shows creating a task dynamically and statically in FreeRTOS.
Look at what happens when creating tasks repeatedly.
- Primary operation: Memory allocation for task control block and stack.
- How many times: Once per task creation.
As the number of tasks (n) increases, each task requires allocation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 allocations |
| 100 | 100 allocations |
| 1000 | 1000 allocations |
Each new task adds a fixed amount of work for allocation, so work grows steadily with the number of tasks.
Time Complexity: O(n)
This means the time to allocate memory grows directly with the number of tasks created.
[X] Wrong: "Static allocation is always faster because it uses fixed memory."
[OK] Correct: Both static and dynamic allocation take similar time per task; static avoids heap fragmentation but does not reduce per-task allocation time.
Understanding how memory allocation time scales helps you design real-time systems that stay responsive as they grow.
"What if we pre-allocate memory for all tasks at system start? How would the time complexity change when creating tasks later?"