How much ram is being used?

mem,

Agreed, that's great.

By the way, the code-clip in my previous post (just above) is missing the final trailing curly brace to close off the loop() function. Just tack on a } at the end.

I'd really like to see compile-time and dynamic run-time memory profiles built into the IDE. It would help troubleshoot lots of newbie grief (... just like mine).

Thanks men, Marc

This thread has been fantastic and very helpful. I am also listing how to look at memory usage at compile time, using the .elf file. I am sure most would know, how to get this info -- the hard way, so this post is for the sake of completeness (of this thread).

My original post is at http://web.goodrobot.com/blog/2009/10/arduino-crashes-and-memory-usage/

I used a slightly different technique to look at the memory usage. I found the memory usage at the compilation time itself like this. First, find the .elf file that the gcc-avr compiler creates (under an applet directory that the arduino GUI creates). And then do nm applet/sketch.elf | sort | less It has the output similar to the following text (only selected extract here). This output is for sketch, that has moved text strings to flash memory using PROGMEM. 00000000 a tmp_reg ..... 000022f6 T loop 00002398 T setup 00002444 T main ..... 00002d82 t __stop_program 00002d84 A __data_load_start 00002d84 T _etext 00002df2 A __data_load_end 00800100 D __data_start 00800134 D crlf ..... 0080078a B timer0_millis 0080078e b timer0_fract 0080078f B __bss_end 0080078f N _end 0080078f N __heap_start 00810000 N __eeprom_end

As you would notice, the data section (bss and data sections included) starts from 0×800100 and ends at 0×80078f, from where the heap and stack occupy. That comes to about 1689 bytes. I am working on Arduino Duemilanove, which has ATmega328 and has 2K RAM. So we are okay there (for now atleast).

Earlier, my data section was stretching upto 0×800900 (about 0×800 bytes which is 2K RAM) and causing crash/hang. None of my code or library uses malloc (as malloc is not listed on the elf file), so my sketch won't need heap memory. In any case, we should just keep the data/bss variables well within 0×800 bytes and keep some free memory too for stack and any library usage of heap.

This thread was extremely helpfull, and mem's MemoryFree library above is a must-have. It should be included in the documented arduino libraries.

MemoryFree.cpp is missing an #include line. Without it, I can't compile the example under arduino 18.

Here's the modified code:

extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;


#include "MemoryFree.h";


int freeMemory() {
  int free_memory;

  if((int)__brkval == 0)
     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);

  return free_memory;
}

I think this lib should be almos-standard, so I'm going to put it in the playground.

Here’s also an example sketch:

#include <MemoryFree.h>

// Reported free memory with str commented out:
// 1848 bytes
//
// Reported free memory with str defined:
// 1834
//
// Difference: 14 bytes (13 ascii chars + null terminator

// 14-bytes string
//char str[] = "Hallo, world!";


void setup() {
    Serial.begin(115200);
}


void loop() {
    // also uncomment this line
    // when uncommenting the str definition line
    // otherwise the unused string will be compiled out
    //Serial.println(str);
    
    Serial.print("freeMemory()=");
    Serial.println(freeMemory());
    
    delay(1000);
}

http://www.arduino.cc/playground/Code/AvailableMemory

So there's just one entry point for the "free memory" problem.