Post navigation

A Comparison of Arduino Sketch Sizes in Memory

A comparison of several Arduino sketches from the Arduino code examples, as well as some code that uses a combination of library functions, shows how much memory is used in an Arduino Uno. This gives a rough idea of what the Arduino Uno is capable of handling in terms of memory usage / sketch size before having to move up to an Arduino that has more memory such as the Arduino MEGA.

Memory Usage in the Arduino IDE

Older versions of the Arduino IDE only showed the amount of Flash memory (non-volatile memory) used by an Arduino sketch at compile time i.e. when the Verify icon is clicked on the toolbar.

Newer versions of the Arduino IDE now show both the amount of Flash memory and SRAM memory that a sketch will use after compiling. The amount of SRAM memory used does not include local variables, however the compiler message shows the amount of SRAM left for local variables.

Arduino IDE Showing Sketch Memory Usage

The above image shows the Arduino IDE after compiling. The area below the sketch shows the messages from the compiler that displays the sketch memory usage.

Arduino Sketch Memory Usage Examples

The examples below show the memory usage reported by the Arduino IDE compiler. Arduino IDE version 1.6.4 compiling for an Arduino Uno is used for all the examples.

Most of the sketches can be found in the Arduino IDE examples under File –> Examples as shown below, a link is given to sketches that are not found in with the IDE examples. The SRAM usage does not include SRAM used by local variables. The Flash memory maximum size of 32,256 bytes is the Arduino Uno Flash memory size of 32,768 bytes, minus 0.5kB used for the boot loader program.

Blinking An LED

File –> Examples –> 01.Basics –> Blink
This simple sketch blinks the on-board LED on and off at a set interval.

Flash usage: 1,030 bytes (3%) of 32,256
SRAM usage: 9 bytes of 2,048

Reading Analog Input

File –> Examples –> 01.Basics –> AnalogReadSerial
Reads a single analog value and prints it to the serial port.

The compiler shows the amount of free memory as 710 bytes available for local variables. The amount of free memory shown by the freeRam() function is 657 bytes at the time that the web page has been sent to the client. This available SRAM now takes into account the local variables that are in use and this is the reason that the runtime RAM available is less than the compile time RAM.

Comparing the Memory Usage Results

From the above memory usage results, it can be seen that some of the simpler sketches use less than 15% of available Flash and less than 15% of available SRAM. These are very simple sketches that only demonstrate a single principle.

The SD card read / write example starts approaching the half way mark of the available memory resources with the web client and simple web server using less than half the memory resources.

As soon as more libraries are used together, memory usage goes over the 50% mark as is the case with the SD card web server at 70% Flash memory usage and 62% SRAM usage. This is still a relatively simple web server hosting a single static web page, but now including the SD card library.

When the LCD is added to the SD card web server, memory usage goes up to 74% for Flash and 65% for SRAM.

From the above, we can see that a simple sketch that uses multiple libraries can quickly use up available memory resources on an Arduino Uno. There are some techniques for making more efficient use of memory. One of the simplest ways to free up RAM is to use the F() macro to keep text in Flash memory so that it does not get copied to RAM at all. The F() macro can be applied to strings that are constant and aren’t modified at runtime.

Freeing Up SRAM

The same sketch above can be modified using the F() macro to put the text strings that are sent from the Arduino over the serial port, to the LCD and to the Ethernet client into Flash memory. For example, the string in the following line of code:

Serial.println("Initializing SD card...");

can be put into Flash memory like this:

Serial.println(F("Initializing SD card..."));

by putting the string including the opening and closing quotation marks into the brackets of the F() macro.

From this comparison of sketches on the Arduino Uno, it can be seen that the main limiting factor on the Arduino Uno is the amount of available memory. A relatively short sketch that uses functions or objects from several libraries can quickly use up 70% of available memory. Any sketch that gets slightly more complex can start approaching the 100% memory usage mark.

When available RAM becomes limited, there is good chance that the sketch can start exhibiting unexpected behaviour and even crash as the unavailability of RAM results in variables being overwritten. One technique has been shown that can help to reduce the amount of RAM used by a sketch.