SRAM Stats

How do I see the amount of SRAM used or free? I want to adjust the size of some large buffers etc while writing the sketch. I write code, fiddle with buffers till it doesn't crash, write some more,etc.

Use this function to determine free RAM…

int freeRam() {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval==0?(int)&__heap_start:(int)__brkval);
}

Maybe this: http://forum.pololu.com/viewtopic.php?f=10&t=989&view=unread#p4218 'll help you!

Thanks for your replies. Is there an addon for the compiler or another compiler that shows stats during design time?

@masterl1nk, That code doesn't compile. If you can post a sketch that will compile it would be appreciated.

Change

extern void __bss_end;

To:

extern uint8_t __bss_end;

However the version linked by masterl1nk is no good if you use malloc or new which catches people out.

The version posted by David is safe for malloc and new use.

Thanks for your replies. I don't understand what the code does, I guess it shows the count in the area at the bottom where it shows the compiled byte count. I will just write a small application that will calc the number of each variable and multiply them by the bytes for each type. It will do a new count whenever it sees the source code change. It's 'non arduino' but it will be a quick fix.

The code looks at the block storage section (BSS) to find out how much heap is free and looks at the address of a local variable v, to see where the stack has grown to - basically free RAM is the gap between the current heap-allocation high-water-mark and the stack pointer (stack grows downward).

Thanks Mark, Counting bytes of all declared variables is giving good results so I'll use that for now. I'll probably buy a full featured C compiler for future projects.

I'll probably buy a full featured C compiler for future projects

Why?

Hello AWOL, I have used the Mikro compiler for PIC for many years and so getting an Arduino project going for someone was a bit of a shock. I'm sure the Arduino compiler is ok for projects that don't have nasty bugs but it has limited fault finding tools, eg can't show stats. Compare the difference between it and the equivalent one here http://www.mikroe.com/mikroc/avr/ide/ I'm not trying to put down the Arduino or the compiler, I'm just saying that there are better compilers for some tasks. My familiarity with the Mikro PIC compiler makes the AVR vrsion an obvious choice.

I'm sure the Arduino compiler is ok for projects that don't have nasty bugs but it has limited fault finding tools, eg can't show stats.

The "Arduino compiler" is a fully-functional avr-gcc compiler.

It functions well to produce code. It does have limited fault finding tools. Please read my post, I'm not trying to knock it. The writer of the project spent days trying to get it working, I spent more than necessary because I couldn't see the memory usage. Reducing the size of his buffers fixed the problem and commenting out code that used lots of int varables also fixed it but seeing how close he was to the limit was not easy. I just kept reducing the size of his buffers (they were byte errorarray[90], etc) until it worked. It also had an unnecessary lot of long strings in SRAM that didn't help eg "Error 34. The motor speed is too high"

My familiarity with the Mikro PIC compiler makes the AVR vrsion an obvious choice.

I had a quick look, but it does not seem to support C++, maybe it does but it raves about C everywhere.

If there is no C++, most of the Arduino core is unusable.

It also had an unnecessary lot of long strings in SRAM that didn't help eg "Error 34. The motor speed is too high"

You know you can simply move them into program memory don't you.

Serial.println(F("This string will be stored in flash memory"));

From:- http://playground.arduino.cc/Learning/Memory

Thanks Mike, that's a handy technique, and easy to access the strings too. I got it working by reducing his need for huge buffers but without your suggestion the only other option would have been to reduce the long strings, thanks.