Cart 0

Optimising SRAM with James Sleeman's Arduino Memory Usage Analyser

Gareth Halfacree

James Sleeman's Arduino RAM usage analyser

Developing software for the Arduino brings with it a number of challenges, not least of which is the amount of memory available. Where a modern desktop computer may have 8GB or more of memory, and even a mobile phone has 512MB or more, the popular Arduino Uno has just 2KB of SRAM built into its ATmega328 microcontroller chip. Even newer boards, such as the joint Intel-Arduino designed Genuino 101, measure the available RAM in kilobytes rather than megabytes.

A microcontroller isn't a PC, though, and the programs you create will require considerably less memory than playing the latest games or browsing a website - but that doesn't mean you won't run into issues with a lack of RAM when your projects begin to get more complex. If you're already at that point, James Sleeman has a tool you might just find handy: a memory usage analyser for AVR programs, including those compiled by the Arduino IDE.

Using James' analyser is simple: it runs entirely within the web browser, with no need to download anything. Just compile your Arduino Sketch as usual, then find the compiled binary - in a 'build' subdirectory of your temporary folder, with a .elf file extension - and upload it. The analyser will then get to work, spitting out a full analysis of the program mere seconds later.

For the beginner the output of James' memory usage analyser can seem overwhelming, but it offers a wealth of information that can help you find areas where the memory usage of your program can be trimmed. The output begins with a look at the global variables and objects, listing each in order of the amount of memory used. If you can see a variable here which doesn't actually need to be global but can instead be changed to a local variable in a particular part of your program, that's an easy win.

The next section finds strings which are placed into SRAM when your program first loads up. These take up a solid chunk of memory, and many can easily be shifted by wrapping them in the F macro to store them in the chip's more capacious flash memory instead. Other areas offer detailed look at the size of each function within your sketch, giving you an overview of just where your program is taking its memory allowances.

If you're not at the stage yet where your Arduino Sketches are running into the edges of SRAM, you don't need James' tool. It's certainly one to bookmark for the future, though, and if your latest project is particularly complex can save you a considerable amount of head-scratching as you work through optimising its memory usage.

Short story... you compile your program (sketch) for your AVR Microcontroller (Arduino) and you see that the reported dynamic memory usage, or flash memory usage of your program is more than, or dangerously close to, the total available ram/flash of your microcontroller. You want to find out who the biggest (global) users of your RAM/FLASH are so you can try and trim some fat.


Older Post Newer Post