This approach works for Arduino / PlatformIO as well as ESP-IDF projects.
#include <esp_heap_caps.h> uint32_t freeHeapBytes = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); uint32_t totalHeapBytes = heap_caps_get_total_size(MALLOC_CAP_DEFAULT); float percentageHeapFree = freeHeapBytes * 100.0f / (float)totalHeapBytes; // Print to serial Serial.printf("[Memory] %.1f%% free - %d of %d bytes free\n", percentageHeapFree, freeHeapBytes, totalHeapBytes);
Also see How to find number of free bytes in ESP32 memory / heap?