Skip to content

10. Free Memory and Memory Allocation Problems

Matthias Prinke edited this page Feb 21, 2026 · 1 revision

To print available memory and check for fragmentation on ESP32 or ESP8266, use these functions:

ESP32:

#include <esp_heap_caps.h>
Serial.printf("Free heap: %u bytes\n", ESP.getFreeHeap());
Serial.printf("Largest free block: %u bytes\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
Serial.printf("Total heap: %u bytes\n", ESP.getHeapSize());

ESP8266:

Serial.printf("Free heap: %u bytes\n", ESP.getFreeHeap());

For fragmentation, compare free heap and largest free block. If largest free block is much smaller than free heap, fragmentation is present.

Add these prints where you want to monitor memory.

Clone this wiki locally