Lost Memory…

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!

 

Author: Helicopter Jeff

I'm a vinyl decal cutting and designing, Astrophotography and general photography capturing, RC helicopter/quad copter pilot, Arduino and Raspberry Pi hardware/software developer, network, scripting and troubleshooting ninja living in the metro NY area. I am passionate about my hobbies so much I do them for a living. There's nothing better than getting paid to do what you love.

1 thought on “Lost Memory…”

  1. Also note that when you put strings in RAM, you are actually storing them twice. Once in Flash, then on boot up, they are copied to RAM. Try defining your strings as const char *. Then the strings won’t be expected to be modified and your code will reference a pointer in the flash area of code instead of RAM.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s