Memory usage monitoring in FreeRTOS - Time & Space Complexity
When monitoring memory usage in FreeRTOS, it is important to understand how the time to check memory changes as the system runs.
We want to know how the cost of checking memory grows as the amount of memory or tasks increases.
Analyze the time complexity of the following FreeRTOS memory check function.
size_t xPortGetFreeHeapSize(void) {
size_t freeHeap = 0;
for (BlockLink_t *pxBlock = pxStart; pxBlock != NULL; pxBlock = pxBlock->pxNextFreeBlock) {
freeHeap += pxBlock->xBlockSize;
}
return freeHeap;
}
This function sums the sizes of all free memory blocks to find total free heap size.
- Primary operation: Loop through each free memory block linked list node.
- How many times: Once for every free block in the heap.
As the number of free memory blocks increases, the time to sum their sizes grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The time grows linearly as the number of free blocks increases.
Time Complexity: O(n)
This means the time to check free memory grows directly with the number of free blocks.
[X] Wrong: "Checking free memory is always a quick constant-time operation."
[OK] Correct: The function must visit each free block, so time depends on how many blocks exist, not fixed.
Understanding how memory monitoring scales helps you design efficient embedded systems and shows you can reason about resource costs clearly.
"What if the free blocks were stored in a balanced tree instead of a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand memory monitoring role
Memory usage monitoring helps detect low memory conditions to avoid crashes.Step 2: Identify the main goal in FreeRTOS
Tracking free stack space per task ensures system stability and prevents overflow.Final Answer:
To keep the system stable by tracking free stack space -> Option AQuick Check:
Memory monitoring = system stability [OK]
- Confusing memory monitoring with CPU speed control
- Thinking it disables tasks automatically
- Assuming it reduces kernel size
Solution
Step 1: Identify stack monitoring function
uxTaskGetStackHighWaterMark() returns the minimum free stack space for a task.Step 2: Differentiate from other task functions
Other functions manage tasks but do not report stack usage.Final Answer:
uxTaskGetStackHighWaterMark() -> Option AQuick Check:
Stack check function = uxTaskGetStackHighWaterMark() [OK]
- Choosing task creation or deletion functions
- Confusing delay function with memory check
UBaseType_t freeStack = uxTaskGetStackHighWaterMark(NULL);
printf("Free stack: %u\n", freeStack);
What does this code output?
Solution
Step 1: Analyze uxTaskGetStackHighWaterMark(NULL)
Passing NULL gets the current task's minimum free stack space since start.Step 2: Understand printf output
It prints the free stack space value, not total stack or CPU info.Final Answer:
The minimum free stack space left in the current task -> Option CQuick Check:
NULL param = current task stack free space [OK]
- Thinking it returns total stack size
- Confusing with task count or CPU usage
UBaseType_t freeStack = uxTaskGetStackHighWaterMark(taskHandle);
if(freeStack == 0)
{
printf("Stack overflow detected!\n");
}
But it never prints the warning even when the system crashes. What is the likely issue?
Solution
Step 1: Understand uxTaskGetStackHighWaterMark() return
It returns minimum free stack space, zero means no free space left.Step 2: Check condition logic
Waiting for exactly zero misses low free space warnings; better to check below a threshold.Final Answer:
Checking for zero instead of a low threshold -> Option BQuick Check:
Zero check misses low stack warnings [OK]
- Assuming zero is the only danger point
- Confusing taskHandle usage
- Blaming printf for missing output
Solution
Step 1: Understand monitoring multiple tasks
Each task has its own stack; checking all ensures no task is overlooked.Step 2: Implement threshold check per task
Looping through tasks and comparing free stack to 50 words allows targeted warnings.Step 3: Evaluate other options
Checking only current or highest priority task misses others; increasing heap doesn't solve stack issues.Final Answer:
Loop through all task handles, call uxTaskGetStackHighWaterMark(), compare to 50, log if below -> Option DQuick Check:
Check all tasks individually for stack [OK]
- Assuming one check covers all tasks
- Ignoring lower priority tasks
- Confusing heap size with stack size
Ready to prove it?
Pass the test and this pattern moves unknown
