Programming the Arduino – Memory Management / Part 2

Let’s get started with the second part of the Arduino programming series. This time we’ll be talking about memory management on the Arduino, how to use it and how best to manage it – showing you how to free up memory and guidelines for programming that will help you reduce your memory footprint on the Arduino.

This article requires some basic understanding of memory, datatypes and basic C/C++ programming skills.

There are 3 types of memory on the Arduino:

  1. Flash Memory – This is where your Arduino Sketch is stored.
  2. SRAM – This memory is used for variables and manipulation.
  3. EEPROM – This is a persistent memory space for long-term storage.

How is this useful? Well, from inside of the Arduino IDE you can use different types of memory to better manage your program, for example if you’re coming to the memory limits of the Arduino then you can use different types of memory for storage.

Normally when you create a variable (for example, a string of “Hello World”) this will get created and stored in the SRAM memory – Having many string variables will eat up your memory limits quite quickly, knowing this we could create the strings at burn-time (Flash memory) instead of initialising them when the code in the sketch executes.

This would mean that string manipulation is limited; you cannot modify a variable in Flash memory directly – It has to be done when the data is burned to the microcontroller, basically when you upload your sketch.

So, what are the memory types and limits?

ATMega168ATMega328PATmega1280ATmega2560
Flash 
(1 Kbyte used 
for bootloader)
16 KBytes32 KBytes128 KBytes256 KBytes
SRAM1024 bytes2048 bytes8 KBytes8 KBytes
EEPROM512 bytes1024 bytes4 KBytes4 KBytes

The above table of information is taken from the Arduino.cc website. This shows how much storage is allocated for each type of memory on the Arduino. As you can see, there is lots of Flash memory to play around with.

Which memory type should I choose for storage?

Good question. It’s entirely up to the purpose of the application. One thing we do point out though, is that the EEPROM memory has a limited number of erase/writes, meaning that if it’s used too much it will eventually not work and come to the end of it’s life, there are no limits on read cycles though.

EEPROM has around 100,000 erase/write cycles before it will eventually come to the end of it’s life. Flash memory (sketch uploads) are rated for around 10,000 erase/write cycles. SRAM has no limits for erase/read/write cycles and is much faster to access than EEPROM or Flash memory.

If you are rapidly changing variables we would suggest using the default SRAM; if you’re storing static strings we would suggest using Flash memory. For data that doesn’t change very often (e.g. program settings) we would suggest EEPROM so that you don’t use many of the erase/write cycles and reduce the life span of the Arduino.

How do I use it?

See the example code below on using the different types of memory. Note that the Arduino IDE now supports the use of the F() which is a helper function for storing data in flash memory. This will cut down bytes in the SRAM memory pool.

 Flash Memory

The other way of storing information in Flash memory is to use the PROGMEM variable modifier (the below is from the Arduino.cc website). Any variables that are using PROGMEM do not get copied into SRAM when the Arduino starts up.

The PROGMEM keyword is a variable modifier, it should be used only with the datatypes defined in pgmspace.h. It tells the compiler “put this information into flash memory”, instead of into SRAM, where it would normally go.

PROGMEM is part of the pgmspace.h library. So you first need to include the library at the top your sketch.

The following code fragments illustrate how to read and write unsigned chars (bytes) and ints (2 bytes) to PROGMEM

You can read more about the PROGMEM variable modifier on the Arduino.cc website by clicking here: http://www.arduino.cc/en/Reference/PROGMEM

EEPROM Usage

The following example code is from the Arduino.cc EEPROM Write reference.

As you can see there is already a built-in library and handy functions for reading and writing to EEPROM. You can view more information and more example code on the Arduino.cc website by clicking here: http://arduino.cc/en/Reference/EEPROM

Also check out a third-party library for EEPROM on the Arduino. This provides handy functions for storing strings and other data types. Check out the project here: http://arduiniana.org/libraries/flash/

SRAM Usage

There isn’t any specific code to use SRAM as any variables you create “normally” will automatically be stored in the SRAM memory space. Anything inside of the SRAM is lost once the Arduino is turned off – The same as RAM in your PC.

You can check how much RAM is available on the Arduino; an example of this is posted below:

The code above is only used for debugging purposes, this shouldn’t be used in an actual sketch. It uses dynamic allocation and deallocation of memory to find out what available RAM is left on the Arduino.

Tips for saving memory

  1. F() all of your strings. This will automatically save some SRAM.
  2. Remove any unneeded variables; functions or #includes.
  3. Array sizing; make sure you’re only allocating what you need. If you allocate a[55] and only use a[5] the memory will still be reserved for that variable and not available for anything else to use.
  4. Use functions. All parameters passed to a function and variables created inside of a function are automatically freed once the function exits!
  5. Use the correct data types. Int, Double, Float – Do you really need the datatype that you’ve allocated?

You can use the below table for comparing data type memory sizes.

datatypes

The one last thing we can think of is that there is a way of taking back some of the used space in the flash memory. There is a HardwareSerial.cpp file located in \hardware\arduino\cores\arduino\ this can be modified to free up more memory.

Look for the line #define SERIAL_BUFFER_SIZE 64 - This can be halved, or more if you are not receiving any high speed data on the Arduino.

If this isn’t enough you can go to the extreme and get rid of the Arduino bootloader. This would mean that the USB connection won’t work and you will have to program the Arduino using an ICSP connection. Take a look at our “Building an Arduino from scratch” article to find out more on this.

Summary

  1. There are 3 types of memory pools on the Arduino. SRAM, Flash Memory and EEPROM
  2. SRAM is used for variables and variable manipulation.
  3. Flash Memory is for storage of the Sketch (this is persistent)
  4. EEPROM is persistent storage
  5. F() is a function used for storing data in Flash memory.
  6. PROGMEM is a variable modifier for storing variables inside of Flash memory.
  7. Flash Memory and EEPROM has a limited number of erase/write cycles.
  8. SRAM is very fast to access, where as Flash memory is slow.
  9. See “tips for saving memory” above for simple programming guidelines to save memory.

We recently found by changing around some programming habits we saved quite a bit of memory in some of our sketches, more so on our Bluetooth project that was 90% Serial.print’s to our Bluetooth COM port.

Hopefully we have covered quite a bit on memory management for the Arduino; if you think we’ve missed anything off or would like to ask any questions, have any comments or suggestions then please feel free to leave a comment below or send us a message using the contact form.

Happy programming!

Email this to someoneShare on Google+Pin on PinterestTweet about this on TwitterShare on RedditShare on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">