如何确定 ESP32 上有多少百分比的内存在堆中是空闲的?

此方法适用于 Arduino / PlatformIO 以及 ESP-IDF 项目。

esp32_heap_percentage_example.cpp

#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;

// 打印到串口
Serial.printf("[Memory] %.1f%% free - %d of %d bytes free\n", percentageHeapFree, freeHeapBytes, totalHeapBytes);

另请查看如何查找 ESP32 内存/堆中的空闲字节数?


Check out similar posts by category: ESP8266/ESP32