0
0
FreeRTOSprogramming~10 mins

Stack high water mark monitoring in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stack high water mark monitoring
Task starts running
Stack usage increases
Check current stack usage
Compare with previous high water mark
Update HWM
Task continues or ends
This flow shows how FreeRTOS tracks the deepest stack usage of a task by checking current usage and updating the high water mark if needed.
Execution Sample
FreeRTOS
UBaseType_t uxTaskGetStackHighWaterMark(TaskHandle_t xTask);

// Called periodically or on demand
highWaterMark = uxTaskGetStackHighWaterMark(taskHandle);
This code calls the FreeRTOS API to get the minimum free stack space (high water mark) for a task.
Execution Table
StepTask StateStack Usage (bytes)High Water Mark (bytes)Action
1Task started01024Initialize stack, HWM set to total stack size
2Running200824Current usage 200, HWM updated to 824
3Running400624Current usage 400, HWM updated to 624
4Running600424Current usage 600, HWM updated to 424
5Running800224Current usage 800, HWM updated to 224
6Running900124Current usage 900, HWM updated to 124
7Running95074Current usage 950, HWM updated to 74
8Running98044Current usage 980, HWM updated to 44
9Running100024Current usage 1000, HWM updated to 24
10Running10204Current usage 1020, HWM updated to 4
11Running10240Stack fully used, HWM updated to 0
12Check HWMN/A0API returns 0 bytes free (stack fully used)
13Task endsN/A0Final high water mark recorded
💡 Task ends or monitoring stops; high water mark shows minimum free stack space during task life
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 10Final
Stack Usage (bytes)02008001020N/A
High Water Mark (bytes free)102482422440
Key Moments - 3 Insights
Why does the high water mark value decrease as stack usage increases?
The high water mark tracks the minimum free stack space left, so as usage grows, free space shrinks, lowering the high water mark (see execution_table steps 2 to 10).
What does the high water mark value represent exactly?
It represents the smallest amount of free stack space remaining during the task's execution, indicating the deepest stack usage (see step 12 where 0 bytes free means stack fully used).
Why is the high water mark useful for monitoring?
It helps detect if the stack size is sufficient or if the task risks overflow by showing the closest point to full stack usage (refer to final high water mark in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 10, what is the stack usage?
A1020 bytes
B1000 bytes
C1024 bytes
D980 bytes
💡 Hint
Check the 'Stack Usage (bytes)' column at step 10 in the execution_table.
At which step does the API report the high water mark as 24 bytes free?
AStep 12
BStep 9
CStep 6
DStep 13
💡 Hint
Look for the step where 'API returns 24 bytes free' in the Action column.
If the stack usage never exceeded 500 bytes, how would the high water mark change?
AIt would stay at 1024 bytes
BIt would be 500 bytes
CIt would be around 524 bytes
DIt would be 0 bytes
💡 Hint
High water mark = total stack size - max usage; check variable_tracker for relation.
Concept Snapshot
FreeRTOS stack high water mark shows minimum free stack space during task run.
Use uxTaskGetStackHighWaterMark(taskHandle) to get it.
Lower value means deeper stack usage.
Monitor to avoid stack overflow.
Update happens as task runs and stack usage changes.
Full Transcript
Stack high water mark monitoring in FreeRTOS tracks the minimum free stack space left during a task's execution. As the task runs and uses more stack, the free space decreases, lowering the high water mark. The API uxTaskGetStackHighWaterMark returns this value, helping developers see how close the task came to overflowing its stack. Monitoring this helps ensure stack sizes are sufficient and prevents crashes. The execution table shows stack usage increasing step by step, while the high water mark decreases accordingly. When the task ends, the final high water mark indicates the smallest free stack space recorded.