SRAM report at compile time

I have recently ran into apparently bizarre problems in a sketch. I would increase a static array size by one element and my sketch would crash as soon as I tried to send data over the serial connection. After some time it dawned that there is a difference between the reported sketch size in the IDE and the SRAM. A long time after the fact I could confirm that the problem lies with overfilling SRAM using freeMemory Arduino Playground - AvailableMemory. The advantages of using freeMemory are obvious, but the downside is that it increases the sketch size. It also only reports SRAM size when it is included, i.e. only after I would suspect something is fishy.

I read an old Arduino issue (#40) which asked for the functionality to report static SRAM usage at compile time. A patch for this has been proposed by user dcb in 2008 (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1224729260) for version 0012. This patch has recently been updated to version 1.01 by Ron Curry in April 2012 (Google Code Archive - Long-term storage for Google Code Project Hosting.). The basis of this patch seems fairly straightforward and although it can only report statically allocated SRAM (since it only sees the allocated memory use at compile time) it will serve as a very handy check. In fact the information that is used by this patch appears to be generated anyway. It is just not obvious, to newbies such as myself, how to access this information.

I see that mellis made a comment about adding it to his todo list in 2008. I guess it is a substantial list, so I would like to bump this patch higher up the list.

Someone had posted a neat little trick that I use now.

If you use windows, the two attachments will help. Type %APPDATA% into explorer and then goto folder Microsoft->Windows->SendTo
Place the two files there.

If you don't have windows you will need to invkoe the avr tools, either manually or some other way.

Then turn on verbose mesages in the IDE options dialog so the compile path is shown.
Once compiled, copy path address into explorer to find the .elf file and right click->sendTo->SRAM.bat

It will produce a file called nm_out.txt listing the memory components, where they sit in memory and their size. ( PGM, SRAM )
SRAM starts at address 00800100.

E.g.

0000062a 00000076 T init
000000dc 0000003e T loop
00000528 0000001e T main
00800110 00000044 B rx_buffer
0000011a 00000012 T setup
008001bf 00000001 b timer0_fract
008001bb 00000004 B timer0_millis
008001b7 00000004 B timer0_overflow_count
00800154 00000044 B tx_buffer

You can see init, loop, main reside in PGM memory, whereas the serial rx_buffer is in SRAM ( 0x44 bytes long )

The second file will produce a text file with the disassembly in it.
it is named sketch.cpp.elf.txt

SRAM.bat (62 Bytes)

AVR Dissasemble.bat (65 Bytes)

Thanks pYro, I'll play around with these options.

Hi pYro, after applying I've got nm_out.txt file with 349 lines long :slight_smile:
Is there any tool which will simply add all SRAM adresses in order to get a SRAM total?
p.

Not that I know of, wouldn't take much to create one.
Look at the options for the avr tool the .bat file uses, it may have an option that will provide this information ( there are many other tools provided in the avr directory. )