I’ve been programming for most of my life. Very very rarely have I had issues with memory or lack of memory.
Now that I’m hot an heavy into microcontroller programming I’ve stumbled across an issue that has apparently been causing me issues without me know it it.
Lack of memory!
The ATmega328 series chip that is the brain of the typical Arduino board has 3 memory locations.
- The Flash is typically 32k bytes. This is what the Arduino IDE tells you about when you compile/upload your code.
- The SRAM is something no one told me about…this is the actual operating program memory space (RAM). This is where your app runs and lives. Your variables live here. When this space is used up things get wonky. The Arduino crashes or worse does strange unexplainable things.
- The EEPROM is generally not used by the average programmer so I won’t cover it here.
JeeLabs has a killer write up on the SRAM and…wait for it….how to see how much is used!
I added their little function to output RAM usage and holy cow. I had it all used up!
So I had to learn about memory management. Lesson one- strings use up a lot of space!
JeeLabs also has a killer write up on how to optimize your string usage. READ IT! The Arduino site also has a write up on how to use the Flash memory for storing static variables too.
After all this learning I finally got my code “optimized”. I thought my latest project was a gonner but putting variables in Flash, dumping A LOT of debug code made a huge difference. I went from zero bytes of RAM and crashes to about 646 bytes free. Doesn’t sound like much but when you only have 2048 bytes to start with, that’s pretty good!
Now to figure out how to have my debug code and still keep the memory usage low!