Get the current free heap with xPortGetFreeHeapSize
Read the bytes currently free in the FreeRTOS heap with xPortGetFreeHeapSize, a total across all free blocks that does not work with heap_3.
xPortGetFreeHeapSize() returns the number of bytes not currently allocated from the kernel heap, which makes it a one-line low-memory check. Call it from a housekeeping task to log heap use as the firmware warms up.
size_t before = xPortGetFreeHeapSize();
void *p = pvPortMalloc(100);
size_t after = xPortGetFreeHeapSize();
printf("took at least 100 bytes: %s\n",
(before - after) >= 100 ? "yes" : "no");
vPortFree(p);took at least 100 bytes: yesThe sample prints a comparison, not the numbers, because the drop is larger than the request: heap_4 adds a block header and rounds to portBYTE_ALIGNMENT. Two limits matter in FreeRTOS Kernel V11.2.0. The figure is the sum of every free block, so it says nothing about the largest allocation that will succeed; fragmentation can leave 2 KB free in pieces too small for a 512-byte request. And heap_3 wraps the C library malloc(), so the kernel cannot see inside it, and xPortGetFreeHeapSize() returns 0 there. For the largest block and the low-water mark, see vPortGetHeapStats() and xPortGetMinimumEverFreeHeapSize() in portable.h.