size_t freeHeap = xPortGetFreeHeapSize(); printf("Free heap size: %u bytes\n", (unsigned int)freeHeap);
The function xPortGetFreeHeapSize() returns the amount of heap memory currently free and available for allocation. It does not show used memory or stack sizes.
FreeRTOS can detect stack overflow if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2. This enables runtime checks to catch stack overflows.
Heap_4 uses malloc and free with coalescing but can still suffer fragmentation if many small allocations and frees happen, leading to heap exhaustion.
The function uxTaskGetStackHighWaterMark() returns the minimum amount of stack space that remained unused. Comparing it to the total stack size helps detect overflow risks.
Using fixed-size memory pools avoids fragmentation because allocations and frees are uniform and predictable, unlike malloc/free which can fragment heap.