ESP32 接上感測器、開了網頁伺服器,測試時一切正常。放著跑了兩天,某天發現它自己重開機過,而且之後每隔一段時間就重開一次。
這種「剛開機沒事,跑久了才出問題」的狀況,第一個要懷疑的就是記憶體。
三個數字
ESP-IDF 的 heap_caps 函式可以看到記憶體的狀態,Arduino 程式直接 #include "esp_heap_caps.h" 就能用:
void printHeap(int round) {
Serial.printf("round %2d free=%6u min_free=%6u largest_block=%6u\n",
round,
heap_caps_get_free_size(MALLOC_CAP_8BIT),
heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT),
heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
}
| 函式 | 意思 |
|---|---|
heap_caps_get_free_size |
目前可用的總量 |
heap_caps_get_minimum_free_size |
開機以來最少曾經剩多少 |
heap_caps_get_largest_free_block |
最大的一塊連續可用空間,一次最多能配置這麼多 |
MALLOC_CAP_8BIT 指一般 malloc、new 用的記憶體。Arduino 的 ESP.getFreeHeap() 也能看可用總量,但只看總量會漏掉後面要講的碎片化。
洩漏:new 了忘記 delete
void handleRequestLeaky() {
char *buf = new char[2048];
snprintf(buf, 2048, "{\"temp\":%d}", 25);
// ... 處理 buf ...
// 忘了 delete[] buf;
}
void loop() {
static int round = 1;
for (int i = 0; i < 10; i++) {
handleRequestLeaky();
}
printHeap(round++);
delay(200);
}
每輪處理 10 次請求,印一次記憶體。Arduino-ESP32 4.0.0、ESP32S3 Dev Module,在 Espressif 的 QEMU 模擬器執行:
round 0 free=364488 min_free=359352 largest_block=303092
round 1 free=343072 min_free=343072 largest_block=294900
round 2 free=321912 min_free=321912 largest_block=278516
round 3 free=300752 min_free=300752 largest_block=258036
round 4 free=279592 min_free=279592 largest_block=233460
round 5 free=258432 min_free=258432 largest_block=212980
round 6 free=237272 min_free=237272 largest_block=192500
round 7 free=216112 min_free=216112 largest_block=172020
round 8 free=194952 min_free=194952 largest_block=151540
round 9 free=173792 min_free=173792 largest_block=131060
round 10 free=152632 min_free=152632 largest_block=108532
round 11 free=131472 min_free=131472 largest_block= 88052
round 12 free=110312 min_free=110312 largest_block= 67572
從第 1 輪之後,每輪固定少 21,160 位元組。10 次 × 2048 位元組是 20,480,每次配置實際多用了 68 位元組,這是配置器管理每個區塊的額外開銷,數字會隨核心的設定不同。
示範用的區塊很大,12 輪就吃掉三分之二。實際專案裡洩漏的通常是幾十、幾百位元組,要幾千幾萬次才會用完,所以會是「跑幾天才當機」。
判斷方式:free 在每輪工作結束後應該回到差不多的值。如果只降不升,就是有東西沒釋放。
修正
#include
void handleRequestFixed() {
std::unique_ptr buf(new char[2048]);
snprintf(buf.get(), 2048, "{\"temp\":%d}", 25);
} // 離開函式時自動 delete[]
round 0 free=364488 min_free=359352 largest_block=303092
round 1 free=364488 min_free=357172 largest_block=303092
round 2 free=364488 min_free=357172 largest_block=303092
...
round 12 free=364488 min_free=357172 largest_block=303092
free 每輪都回到 364488。min_free 從 359352 降到 357172 是正常的:執行當下確實借用了 2048 位元組,用完才還。
只在函式裡暫時用一下的緩衝區,最簡單的做法是直接用區域陣列 char buf[256];,放在堆疊上,連配置都不用。大的才需要用 heap,這時用 std::unique_ptr、std::vector、String 這類會自動釋放的型別,不要自己 new、delete。
碎片化:總量夠,卻配置不到
洩漏修好了,還有另一種狀況:free 看起來很多,配置卻失敗。
std::vector keep;
// 用 8000 位元組的區塊把記憶體配置到滿
while (void *p = malloc(8000)) keep.push_back(p);
printHeap(1);
// 每隔一塊釋放一塊:一半的記憶體還給系統,但彼此不相連
for (size_t i = 0; i 60000) {
last = millis();
Serial.printf("[%lu s] free=%u min=%u largest=%u\n",
millis() / 1000,
heap_caps_get_free_size(MALLOC_CAP_8BIT),
heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT),
heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
}
// ...
}
每分鐘一筆,跑一個晚上,看 free 有沒有持續往下、largest 有沒有越來越小。有網路的話,也可以把這三個數字送到 MQTT 或網頁上,不用接著電腦也看得到。
發表留言