How can we measure the amount of RAM consumed by a code in Arduino Mega 2560

I need to measure the amount of RAM used by a particular code on Mega 2560. Is there any command line action to do so? Or if any other method is there?

Please suggest and help me out.

If you use Sublime Text with the Stino Arduino Package, it will tell you both pieces of information every time you verify/compile your project.

http://forum.arduino.cc/index.php?topic=138014.0

I used this method, which may be similar to what AWOL posted links to.

http://electronics.stackexchange.com/questions/24269/checking-memory-footprint-in-arduino

Great link C2. I did the test, and the RAM usage reported by Sublime was identical to the command prompt value, and the Arduino IDE sketch size was identical to the FLASH usage from command prompt, however, the Sublime FLASH value was 454 bytes larger. Would this be some sort of bug in the Stino code? Or is there some overhead that comes with using it? No worries if you don't know, just curious why Sublime has the correct RAM values but not FLASH.

I don't know, but maybe the Sublime compiler routines are not as optimal?

This is what I use with my Mega 2560. When it runs out of SRAM (not program memory), it displays a negative or an unrealistic positive value.

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

void setup() {
  Serial.begin(9600);
  Serial.print(F("SRAM = "));
  Serial.println(freeRam());
}

void loop() {
}