0
0
FreeRTOSprogramming~5 mins

Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION) in FreeRTOS - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION)
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of tasks (n) increases, each task requires allocation.

Input Size (n)Approx. Operations
1010 allocations
100100 allocations
10001000 allocations

Each new task adds a fixed amount of work for allocation, so work grows steadily with the number of tasks.

Final Time Complexity

Time Complexity: O(n)

This means the time to allocate memory grows directly with the number of tasks created.

Common Mistake

[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.

Interview Connect

Understanding how memory allocation time scales helps you design real-time systems that stay responsive as they grow.

Self-Check

"What if we pre-allocate memory for all tasks at system start? How would the time complexity change when creating tasks later?"