[英]Arduino is not freeing up RAM memory
我正在使用帶有MD5庫的Arduino mega和帶有SoftwareSerial的ESP8266。 問題是370循環后,由於內存不足,Arduino自動重啟。 我使用FreeMemory進行故障排除,我注意到問題是每個循環的可用內存減少了。 這是奇怪的行為,因為它只在我使用AT命令和MD5時出現,但是如果我在兩個草圖中分離草圖它們正常工作而沒有內存問題。 我的原始草圖是一個相當復雜的,但我簡化為實例所示的基本代碼,以便更清楚,行為是相同的,所以如果我修復它,我將能夠修復我的原始草圖
#include <SoftwareSerial.h>
#include <MemoryFree.h>
#include <MD5.h>
void setup() {
// initialize the digital pin as an output.
Serial.begin(115200);
Serial.println("Starting");
Serial1.begin(115200);
delay(200);
}
// the loop routine runs over and over again forever:
void loop() {
Serial1.println("AT");
delay(100);
Serial.println(Serial1.readString());
Serial.println("-----------");
unsigned char* hash=MD5::make_hash("hello world, this an example");
//generate the digest (hex encoding) of our hash
char *md5str = MD5::make_digest(hash, 16);
//print it on our serial monitor
Serial.println(md5str);
//Give the Memory back to the System if you run the md5 Hash generation in a loop
free(md5str);
Serial.println(freeMemory());
}
謝謝!
這是make_hash
的源代碼:
unsigned char* MD5::make_hash(const void *arg)
{
MD5_CTX context;
unsigned char * hash = (unsigned char *) malloc(BLOCK_SIZE);
MD5Init(&context);
MD5Update(&context, arg, strlen((char*)arg));
MD5Final(hash, &context);
return hash;
}
如您所見,返回的hash
變量中有一個malloc()
。 因此,您應該在每次循環迭代結束時調用free(hash)
。
如果需要保持hash
,請將其放在全局范圍內,並在setup()
函數中僅創建一次 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.