Electronic – arduino – Checking memory footprint in Arduino

arduinomemory

I'm working on a simple project with my Arduino. Recently I had to convert one of my variables to a long instead of an int, and in order to keep things simple I just moved over all the numbers that it interacts with (so I don't have to worry about cross-type comparisons and other math). This seems wasteful, but it's just a clock for myself, and I don't care that much.

It has, however, made me wonder about how much memory I am using. I doubt it's an issue, but I realized I don't know any way to check.

So, is there a way to check the amount of memory being used by an Arduino?

Ideally, I'd like to print out the current memory / total available over the serial connection.

Best Answer

A lot of the arduino stuff is handled underneath. You will see how much FLASH memory a sketch (program) will take upon compilation, but not the amount of SRAM memory. I recently had to discover this as well, and this is how I did it.

In the arduino IDE directory there is the avr-gcc compiler. It also contains a tool named 'avr-size'. Go to hardware/tools/avr/bin/ and it should be there.

When compiling, the IDE will create a temporary directory in your temp directory and copy all the C(++) files to it. It will compile the libraries, your sketch (it also adds the required things to make it functional) and create a hex and elf file. What you need to do is compile your sketch (by either clicking upload or verify) and look for a build[hash tag] directory in your temp folder. You need to run avr-size.exe and hand it the elf file.

It's quite tedious console work, but I usually navigate to the bin folder, run avr-size.exe and type the path to my temporary folder. This is C:\Users[your username]\AppData\Local\Temp\build[random tag][Sketch name].cpp.elf (Windows 7)

You could probably make up some (batch) script for this, but as the build folder remains constant on each arduino session, I don't bother.

The output of avr-size looks very similar to this (I have compiled an Xbee example program). Console output

Where:

  • text - flash data used for code
  • data - Memory with initialized data (the initial value has to be stored in FLASH too!)
  • bss - Memory that is initialized with zero's (the compiler will add some code so it will initialize data & bss)
  • dec & hex are a decimal and hex display of the combined RAM and FLASH size of your program.

The Arduino IDE reported to me this program is 3956 bytes big (FLASH). That's related to 3924 (code flash) + 32 (initial RAM values) = 3956 bytes FLASH. The RAM usage is data+bss combined(!) = 32+320 = 352 bytes SRAM usage.

Note the ATMEGA328 only has 2KiB of SRAM, and problems can already occur if you're below that (I've had unexpected behaviours at 1700 bytes out of 2048). The ATMEGA168 has 1KiB of SRAM. All Arduino mega's have got 8KiB of SRAM.

As an additional note. There are also 'free memory indicators' code available. What they do is try to allocate as much memory , count how big it was and free it up (malloc() & free()). If you initially haven't used malloc yet, this will add more code to your program you sometimes don't have space for. This is not ideal and I neither have found it to give stable results. In my own project I had ethernet+xbee+SD card + own library code running on an Atmega328. There just wasn't enough SRAM available, and FLASH became scarce as well).

And last but not least, this indicator doesn't account for memory allocated with malloc().