Checking RAM Memory usage

Started by ColinR, September 08, 2014, 01:40:10 PM

ColinR

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 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
CuPID Controls :: Open Source browser-based sensor and device control
Interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

Felix

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

ColinR

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
CuPID Controls :: Open Source browser-based sensor and device control
Interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

ColinR

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);
    }
CuPID Controls :: Open Source browser-based sensor and device control
Interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com