Can we know how much RAM a sketch uses?

Hi All…

Is there a way to know how much RAM a sketch is using?

http://www.google.com/search?q=free+ram+site%3Aarduino.cc

http://arduino.cc/forum/index.php/topic,50924.msg363682.html#msg363682

Good links from Coding Badly

A way of determining static ram usage is to run the "avr-size" tool on the .elf file that is generated during compilation, look up the manual on "avr-size" on how to use it, and you'll want verbose build output while building with arduino so you know where the .elf file is

However this method does not really take stack and heap into account

Also read http://www.nongnu.org/avr-libc/user-manual/malloc.html

I use a debug function taken from this forum and updated a bit.
It seems to work fine for me :slight_smile:

/*
DebugUtils.h - Simple debugging utilities.

Ideas taken from:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271517197

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef DEBUGUTILS_H
	#define DEBUGUTILS_H

	#ifdef DEBUG
		#include <WProgram.h>
		#define DEBUG_PRINT(str)    \
			Serial.print(millis());     \
			Serial.print("ms: ");    \
			Serial.print(freeRAM());    \
			Serial.print("B: ");    \
			Serial.print(__PRETTY_FUNCTION__); \
			Serial.print(' ');      \
			Serial.print(__FILE__);     \
			Serial.print(':');      \
			Serial.print(__LINE__);     \
			Serial.print(' ');      \
			Serial.print(str);

		#define DEBUG_PRINTLN(str)    \
			Serial.print(millis());     \
			Serial.print("ms: ");    \
			Serial.print(freeRAM());    \
			Serial.print("B: ");    \
			Serial.print(__PRETTY_FUNCTION__); \
			Serial.print(' ');      \
			Serial.print(__FILE__);     \
			Serial.print(':');      \
			Serial.print(__LINE__);     \
			Serial.print(' ');      \
			Serial.println(str);

		#define DEBUG_PRINTDEC(x)    \
			Serial.print(millis());     \
			Serial.print("ms: ");    \
			Serial.print(freeRAM());    \
			Serial.print("B: ");    \
			Serial.print(__PRETTY_FUNCTION__); \
			Serial.print(' ');      \
			Serial.print(__FILE__);     \
			Serial.print(':');      \
			Serial.print(__LINE__);     \
			Serial.print(' ');      \
			Serial.println(x, DEC);

	#else
		#define DEBUG_PRINT(str)
		#define DEBUG_PRINTLN(str)
		#define DEBUG_PRINTDEC(x)
	#endif
#endif

int freeRAM() 
{
  int size = RAMEND;
  byte *buf;
  while ((buf = (byte *) malloc(--size)) == NULL);
  free(buf);
  return size;
}

Example of use:

///////////////////////////////////////////////////////////////////////////////
// Define DEBUG to produce verbose output to console
// Note: _DO NOT_ define DEBUG if using digital pins 0/1 for something else
#define DEBUG
#include "DebugUtils.h"
void setup()
{
  #ifdef DEBUG
    Serial.begin(115200);
  #endif
    DEBUG_PRINT("Booting sketch.");
    DEBUG_PRINTLN(", continues the line above");
}

void loop()
{}

I wanto to expand the debug library to bit-bang any pins you want to use for serial output, but the newbnewss factor with C++ and Arduino get in the way.

sjfaustino: I use a debug function taken from this forum and updated a bit. It seems to work fine for me :)

Example of use:

I wanto to expand the debug library to bit-bang any pins you want to use for serial output, but the newbnewss factor with C++ and Arduino get in the way.

Just use software serial, newsoftserial or something like that

Great info, thanks everyone.

It turned out that the Fat16 library I am using to talk to my SD card included a function to report free memory. That works well. Its the same function as in one of the links above.