MemoryFree reports memory usage on an Arduino
Ed – I couldn’t put it better myself, so here it is, straight from the author’s mouth.
Hey Dan,
I’ve attached the free open source Arduino libray called MemoryFree I mentioned to you last night. Add it to your Arduino libraries, then:
[code]
#include <MemoryFree.h>
void printMemoryStats()
{
Serial.println();
Serial.print(F("Available memory: "));
Serial.print(freeMemory());
Serial.println(F(" bytes."));
Serial.println();
Serial.flush();
}
[/code]
You can then scatter calls to printMemoryStats() around your code to narrow down the problem areas. When I was tracking down the problems with the VHS doorbot, I ended up with calls to printMemoryStats() inside each set of curly braces, after variables, in many functions, etc (I also put calls in a number of other libraries I was using, as that’s where it was actually failing).
It let me find the problem *really* quickly (fixing it wasn’t quite so quick though :P). The more calls you put in, the easier you’ll be able to see if something is taking up an unexpectedly large amount of memory – because obviously it may just run out of memory way down in the call stack where it’s not using much additional memory, but only because a higher-level function used a lot.
Good luck!
-Richard