February 4, 2011 1 Comment
Do you ever wonder how much memory your arduino has and should you worry about it running out? Here is the answer:
There are three types of memory on the arduino’s main chip, the ATMEGA328 chip:SRAM, flash RAM, and EEPROM. Here is what they do:
SRAM holds all the variables you define, including integers, floats, strings, objects, etc, as well as the stack, the space used in function calls. There are only 2KB or 2048 bytes of SRAM on board so one should make some effort to save this memory. This RAM is volatile meaning its content is wiped clean when power is removed.
Flash RAM holds the arduino bootloader and the entire compiled program. The total size is 32KB or 32768 bytes. The bootloader is typically 2KB or less, leaving about 30KB for your compiled program. This memory is checked by Arduino IDE and will not be run out without the IDE complaining to you first. From my experience, around 100 lines of C code will consume 1KB or flash RAM. The initial value of a variable is also stored in the Flash RAM. If you have char msg=”Hello”; in your program. First it shows up on the Flash RAM as initial value of msg being “Hello”, then when the program starts running, “Hello” will be copied to the SRAM so taking space in both flash and SRAM. This is unfortunate for variables like an integer but constant strings don’t change their values so they don’t need to reside in SRAM at all time. With a mechanism, you can keep them in flash until you need them. Then load them to SRAM, use the string, then dump it. Flash RAM is non-volatile meaning its content is retained in case power is removed.
EEPROM is electrically erasable programmable read-only memory. It is used to store parameters and small amount of data so that every time arduino starts up, it can load the parameters and data. Then you can change these parameters and save them to the EEPROM so your changes won’t be lost when you power off. There are 1KB or 1024 bytes of EEPROM on ATMEGA328, the chip that UNO and Duemilanove use. EEPROM is slow so it is not practical to store variable on EEPROM.
For suggestions on how to optimize your arduino memory, read this post.