Memory usage monitoring helps you see how much memory your FreeRTOS program uses. It keeps your system running smoothly by avoiding memory problems.
Memory usage monitoring in FreeRTOS
Introduction
Syntax
FreeRTOS
size_t uxTaskGetStackHighWaterMark(TaskHandle_t xTask);
This function returns the minimum amount of stack space that has remained for the task since it started.
You need to pass the task handle to check its stack usage.
Examples
FreeRTOS
size_t freeStack = uxTaskGetStackHighWaterMark(NULL);
FreeRTOS
size_t freeStack = uxTaskGetStackHighWaterMark(myTaskHandle);
Sample Program
This program creates a task that prints its free stack space every second. It helps monitor memory usage live.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskFunction(void *pvParameters) { for (;;) { size_t freeStack = uxTaskGetStackHighWaterMark(NULL); printf("Free stack space: %u words\n", (unsigned int)freeStack); vTaskDelay(pdMS_TO_TICKS(1000)); } } int main(void) { xTaskCreate(vTaskFunction, "Task1", 100, NULL, 1, NULL); vTaskStartScheduler(); return 0; }
Important Notes
Stack space is measured in words, not bytes. One word is usually 4 bytes on many systems.
Low free stack values mean your task is close to running out of memory.
Use this monitoring to avoid stack overflow crashes.
Summary
Memory usage monitoring in FreeRTOS helps keep your system stable.
Use uxTaskGetStackHighWaterMark() to check free stack space per task.
Regular monitoring prevents crashes and helps optimize memory use.
Practice
1. What is the primary purpose of memory usage monitoring in FreeRTOS?
easy
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]
Hint: Memory monitoring prevents crashes by tracking stack space [OK]
Common Mistakes:
- Confusing memory monitoring with CPU speed control
- Thinking it disables tasks automatically
- Assuming it reduces kernel size
2. Which FreeRTOS function is used to check the minimum amount of stack space left for a task?
easy
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]
Hint: Stack water mark means free stack space left [OK]
Common Mistakes:
- Choosing task creation or deletion functions
- Confusing delay function with memory check
3. Given the code snippet:
What does this code output?
UBaseType_t freeStack = uxTaskGetStackHighWaterMark(NULL);
printf("Free stack: %u\n", freeStack);
What does this code output?
medium
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]
Hint: NULL means current task in FreeRTOS stack check [OK]
Common Mistakes:
- Thinking it returns total stack size
- Confusing with task count or CPU usage
4. You wrote this code to monitor stack usage:
But it never prints the warning even when the system crashes. What is the likely issue?
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?
medium
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]
Hint: Check for low stack threshold, not just zero [OK]
Common Mistakes:
- Assuming zero is the only danger point
- Confusing taskHandle usage
- Blaming printf for missing output
5. You want to monitor stack usage for multiple tasks and log warnings if any task's free stack falls below 50 words. Which approach is best?
hard
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]
Hint: Check each task's stack individually for accurate monitoring [OK]
Common Mistakes:
- Assuming one check covers all tasks
- Ignoring lower priority tasks
- Confusing heap size with stack size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
