Evaluating the static and dynamic memory consumption for AVR microcontroller programs

RAM memory is probably the most valuable resource on a microcontroller. For example, Atmel’s 8-bit MCU ATmega128 offers only 4 KB of SRAM memory, which is shared by the data area, BSS segment, heap (if present) and stack. A major challenge in embedded programming is that there is no protection or detection of a memory violation. Therefore, it is best practice to use mostly static memory in your embedded programs. The amount of statically reserved memory can be derived from the compiled program using the avr-size command:

$ avr-size ExampleMemEval.elf
text    data     bss     dec     hex filename
1474     156       0    1630     65e ExampleMemEval.elf

In the above example there are 156 bytes of RAM memory statically used (and 1474 bytes of Flash ROM).

However, how to evaluate the total memory consumption of your program including the non-static part? In order to deduce this dynamic RAM usage, the best thing is to use the utility program mem_eval. It provides a function, InitializeMemory, that initializes the memory with a known byte pattern before your actual application starts. After the application has run for some time (hopefully covering an execution mode with high memory consumption), the function AnalyzeMemory can be used to check how much memory is still unused. The function borrows only a couple of bytes of RAM memory, which gives a negligible probe effect. AnalyzeMemory is searching for the largest block that appears to be unchanged and reports the result. If the amount of unused space is just a few bytes, your variables and your stack are likely to overwrite each other which can manifest in sporadic miscalculations, program crashes, or any other unexpected behavior.

This program is distributed under the terms of the GNU Lesser General Public License. Get mem_eval here!


About Wilfried Elmenreich

Understanding the communication networks of the future.
This entry was posted in Dependable Systems, Embedded Software, Real-Time Networks and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s