FreeRTOS heap implementations (heap_1 to heap_5) - Time & Space Complexity
When using FreeRTOS, memory allocation speed affects how fast tasks start and run. Different heap implementations manage memory differently, so their speed changes as memory requests grow.
We want to know how the time to allocate or free memory changes when the number of allocations increases.
Analyze the time complexity of the heap_4 memory allocation function.
void *pvPortMalloc( size_t xSize ) {
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
void *pvReturn = NULL;
if( xSize > 0 ) {
// Traverse free list to find a block big enough
pxPreviousBlock = &xStart;
pxBlock = xStart.pxNextFreeBlock;
while( pxBlock != NULL && pxBlock->xBlockSize < xSize ) {
pxPreviousBlock = pxBlock;
pxBlock = pxBlock->pxNextFreeBlock;
}
// If found, allocate and adjust free list
if( pxBlock != NULL && pxBlock->xBlockSize >= xSize ) {
pvReturn = (void *)( ( uint8_t * ) pxBlock + heapSTRUCT_SIZE );
// Adjust free list here
}
}
return pvReturn;
}
This code searches a linked list of free memory blocks to find a block large enough to allocate requested memory.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the linked list of free memory blocks.
- How many times: Up to the number of free blocks in the heap.
As the number of free blocks grows, the search takes longer because it checks each block until it finds a fit.
| Input Size (number of free blocks) | Approx. Operations (block checks) |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The time grows roughly in direct proportion to the number of free blocks.
Time Complexity: O(n)
This means the time to allocate memory grows linearly with the number of free blocks in the heap.
[X] Wrong: "Memory allocation always takes constant time regardless of heap size."
[OK] Correct: In FreeRTOS heap implementations like heap_4, allocation searches through free blocks, so more blocks mean more time.
Understanding how memory allocation time grows helps you explain system responsiveness and resource management in embedded systems.
"What if the free blocks were kept in a sorted tree instead of a linked list? How would the time complexity change?"
Practice
NOT support freeing allocated memory?Solution
Step 1: Understand heap_1 behavior
heap_1 only allows allocation but does not provide a function to free memory once allocated.Step 2: Compare with other heaps
heap_2, heap_3, and heap_4 all support freeing memory, with heap_3 using standard malloc/free.Final Answer:
heap_1 -> Option AQuick Check:
heap_1 no free support = heap_1 [OK]
- Confusing heap_1 with heap_2 which supports free
- Assuming all heaps support freeing memory
- Mixing heap_3's malloc/free with heap_1's behavior
Solution
Step 1: Check FreeRTOS heap file naming
Heap implementations are included by adding the corresponding .c file, e.g., "heap_3.c".Step 2: Verify correct syntax
Using #include "heap_3.c" is the standard way to include heap_3 in the build.Final Answer:
#include "heap_3.c" -> Option BQuick Check:
Include heap_3.c with quotes = D [OK]
- Using angle brackets instead of quotes
- Incorrect file name spelling or casing
- Trying to include a header instead of the .c file
void *ptr = pvPortMalloc(1000);
if(ptr == NULL) {
printf("Allocation failed\n");
} else {
printf("Allocation succeeded\n");
}Solution
Step 1: Understand heap_4 allocation behavior
heap_4 supports allocation and freeing with coalescing. If memory is insufficient, pvPortMalloc returns NULL.Step 2: Analyze the if condition
If ptr is NULL, the code prints "Allocation failed"; otherwise, it prints "Allocation succeeded".Final Answer:
Allocation failed -> Option CQuick Check:
pvPortMalloc NULL means failure = A [OK]
- Assuming allocation always succeeds
- Confusing NULL with zero pointer
- Ignoring the if condition logic
Solution
Step 1: Recall heap_2 features
heap_2 supports freeing memory via vPortFree(), but does not coalesce free blocks.Step 2: Identify common mistake
If memory is not freed, the most common cause is forgetting to call vPortFree() after allocation.Final Answer:
You forgot to call vPortFree() after allocation -> Option DQuick Check:
Missing vPortFree causes leaks = B [OK]
- Assuming heap_2 lacks free support
- Confusing heap_2 with heap_1 or heap_3
- Thinking heap_2 uses malloc/free internally
Solution
Step 1: Review heap features
heap_1 does not support freeing; heap_2 supports free but no coalescing; heap_4 supports free and coalesces adjacent free blocks; heap_5 extends heap_4 with multiple memory regions.Step 2: Match requirements
For minimizing fragmentation and real-time suitability, heap_4 is ideal as it coalesces free blocks and is deterministic.Final Answer:
heap_4 -> Option AQuick Check:
heap_4 coalesces free blocks = A [OK]
- Choosing heap_5 without needing multiple regions
- Assuming heap_2 coalesces free blocks
- Picking heap_1 which lacks free support
Ready to prove it?
Pass the test and this pattern moves unknown
