1

I use this Library to measure my free RAM on a Mega in runtime. But this Library does not work on the Due.

1

2 Answers 2

1

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).

2
  • It seems to work fine after a call to malloc(), which is needed to initialize __brkval. I would return &top - ( __brkval ? __brkval : &__heap_start); in order to have something that works also when not using malloc(). Commented Oct 25, 2016 at 9:34
  • I thought it might be something like that. After all, not everyone uses malloc.
    – Nick Gammon
    Commented Oct 25, 2016 at 10:50
0

Try to use this method:

int freeRAM() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
1
  • 1
    The question was about a Due. When I tried your code on my Due I got -28 bytes of RAM, which is hard to believe.
    – Nick Gammon
    Commented Oct 25, 2016 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.