I use this Library to measure my free RAM on a Mega in runtime. But this Library does not work on the Due.
asked Oct 24, 2016 at 14:12
-
Try this library github.com/mpflaga/Arduino-MemoryFreeMikael Patel– Mikael Patel2016年10月24日 15:47:10 +00:00Commented Oct 24, 2016 at 15:47
2 Answers 2
As mentioned by Mikael Patel in a comment, the library from Arduino-MemoryFree should do it. A minimal sketch that seems to work on the Due:
extern "C" char* sbrk(int incr);
int freeMemory() {
char top;
return &top - reinterpret_cast<char*>(sbrk(0));
}
void setup ()
{
Serial.begin (115200);
Serial.println ();
Serial.print ("Free memory is: ");
Serial.println (freeMemory ());
} // end of setup
void loop () { }
I'm not totally convinced that this gives the correct figure. The figure returned by the library above for my Uno was too high (2297 bytes).
answered Oct 25, 2016 at 8:54
-
It seems to work fine after a call to
malloc()
, which is needed to initialize__brkval
. I wouldreturn &top - ( __brkval ? __brkval : &__heap_start);
in order to have something that works also when not usingmalloc()
.Edgar Bonet– Edgar Bonet2016年10月25日 09:34:17 +00:00Commented Oct 25, 2016 at 9:34 -
I thought it might be something like that. After all, not everyone uses
malloc
.2016年10月25日 10:50:36 +00:00Commented Oct 25, 2016 at 10:50
Try to use this method:
int freeRAM() {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
answered Oct 25, 2016 at 4:12
-
1The question was about a Due. When I tried your code on my Due I got -28 bytes of RAM, which is hard to believe.2016年10月25日 08:36:46 +00:00Commented Oct 25, 2016 at 8:36
lang-cpp