LowPowerLab Forum

Hardware support => Moteino => Topic started by: ColinR on September 08, 2014, 01:40:10 PM

Title: Checking RAM Memory usage
Post by: ColinR on September 08, 2014, 01:40:10 PM
Hey all,

This is more of a general Arduino question, but relevant.

How do you check available memory during runtime?

I have noticed that in larger programs (this one (https://github.com/iinnovations/iicontrollibs/blob/master/mote/remote/uni_mote2p2/uni_mote2p2.ino) in particular), if I add totally irrelevant code, other things stop working.

For example, in the above sketch, if I add an include for a LCD Segment library and simple send value to display command, the program stops responding to serial commands in a way that indicates it's not storing command strings. Specifically, if the sketch doesn't see a command string it recognizes, it returns the command string to serial output. If I do 'certain things' to the program, this does not happen.

I know the answer to the above is optimizing memory usage, but I need a way to track my progress. Any ideas?

Thanks,
Colin
Title: Re: Checking Memory
Post by: Felix on September 08, 2014, 03:16:16 PM
Googling "Arduino RAM usage" reveals lots of links. I think I tried this myself several times with great success, but can't remember if I ever saved the actual code.
Here's something that I would think is reliable: https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory
Title: Re: Checking Memory
Post by: ColinR on September 08, 2014, 03:18:12 PM
I did only some brief googling previously and wasn't really satisfied with what I ended up with. I asked because I figured somebody must have found something useful. I'll take another look.

C
Title: Re: Checking RAM Memory usage
Post by: ColinR on September 09, 2014, 12:49:21 PM
The above link did turn out to be quite helpful. Converting everything to local where possible and minimizing variable spaces helped me lose a lot of weight. The function below is the key diagnostic:

    int freeRam ()
    {
    extern int __heap_start, *__brkval;
    int v;
    return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
    }