Go Down

Topic: Any information on SRAM usage after compilation? (Read 1 time) previous topic - next topic


Hi there,

My Mega 2560 board should have 8kB of SRAM. I wrote a very comprehensive program of about 200k program size that uses up also a large SRAM space. It is getting really tight now and I need to squeeze more SRAM space.
The problem I have is; the compiler doesn't give any information on the SRAM usage and the compilation is always successful even the memory usage has already over 8kB, this results in crash when running some sections of code.
May I ask if there is a possibility the compiler can give out information on total SRAM usage for both static buffers, dynamically created buffers or even the stack size?



The compiler has no idea what functions may be called, and cannot predict stack usage. If you know any computer science, this is equivalent to the halting problem.


Feb 21, 2014, 04:11 pm Last Edit: Feb 21, 2014, 05:22 pm by saravananm92 Reason: 1
In fact yes, but its a little long route and I just read about it today.

1) You can enable verbose mode in preferences.txt to find out where the temporary files are stored (you have to hold shift key during compilation to view it).

2)Now navigate to the temporary folder through commandline.

3)You will see a file with following format in there <sketch_name>.cpp.elf

Code: [Select]
avr-size -C --mcu=m2560 <sketch_name>.cpp.elf in commandline and hit enter. Hopefully it should return the information you want.Test this and reply back.


I can provide some update of this problem. I hope that it will be helpful for somebody.

Utility avr-size evaluates size of constant data and unitialized global and static data. As was mentioned above, form avr-size output, sum of data and bss provide this result. But there is no info about  data stored in stack and allocated data in heap, which are dynamically creted/erased during program run (see AVR Libc help, chapter "Memory Areas and Using malloc()" for details). Compiler doesn't know anything about it, of course. There is a way how to analyse free memory during program run, here is an example:

Code: [Select]

// --------------------------------------------------------------------------------------
// **************
// *  FreeSRAM  *
// **************
// Evaluate an amount of SRAM free space (include free fragments in heap)


/* free list structure as maintained by the avr-libc memory allocation routines */
struct __freelist {
size_t sz;
struct __freelist *nx;

unsigned int FreeSRAM()
unsigned int free_memory = (unsigned int)&free_memory + sizeof(free_memory);
// the last element in stack, so its address is
// current pointer to end of stack

#if defined (__AVR__)

extern unsigned int __heap_start; // beginning of allocation heap
extern void *__brkval; // current end of heap (0 if no allocation yet)
extern struct __freelist *__flp; // head of the free list structure

if(__brkval == 0) free_memory -= (unsigned int)&__heap_start;
  else {          free_memory -= (unsigned int)__brkval;

/* add all free memory fragments if any */
for(struct __freelist *current = __flp; current; current = current->nx) {
free_memory += 2; // two bytes for the memory block's header
free_memory += (unsigned int)current->sz;



/*                           *** ATmega2560 Data Memory ***

0x0000 - 0x001F 32 Registers (1 byte each)
0x0020 - 0x005F 64 I/O Registers
0x0060 - 0x01FF 416 Ext I/O Registers
0x0200 - 0x21FF Internal SRAM 8192 bytes
0x2200 - 0xFFFF External SRAM 0 - 64 kb

Internal SRAM:

0x0200 - bottom of RAM .data variables (contains static data which was
defined in code)
__data_end == __bss_start .bss variables  (uninitialized global or static
variables end up in the .bss section)
__bss_end  == __heap_start heap for dynamic allocation - malloc()
__brkval end of heap
... free memory
SP (stack pointer) local variables
0x21FF - top of RAM (RAMEND) stack is started from the top of internal RAM

#endif // _DEBUG_FREE_SRAM

ATmega2560 has a feature which allows to connect and use an external SRAM (see datasheet for details). I found this: http://andybrown.me.uk/wk/category/arduino/ on web. There is nice example how to expand ATmega2560's memory size.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Go Up