#include <Streaming.h>
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void setup()
{
Serial.begin(115200);
Serial << "1. RAM: " << freeRam () << "\n";
byte *ptr1= (byte*)malloc(1024*sizeof(byte));
Serial << "2. RAM: " << freeRam () << "\n";
byte *ptr2= (byte*)malloc(1024*sizeof(byte));
Serial << "3. RAM: " << freeRam () << "\n";
free(ptr2);
Serial << "4. RAM: " << freeRam () << "\n";
free(ptr1);
Serial << "5. RAM: " << freeRam () << "\n";
}
void loop()
{
}
使用Mega2560測試,Mega2560內建 SRAM 8KB = 8192 Byte
下圖是程式執行結果,可以看出記憶體容量的變化情形
參考資料
http://playground.arduino.cc/Code/AvailableMemory
https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory
http://stackoverflow.com/questions/8649174/checking-memory-footprint-in-arduino
http://arduino.cc/en/Main/arduinoBoardMega2560
沒有留言:
張貼留言