memory usage? problem with Sketch over 8200 bytes

Hi at all,

is there someone that know a system to measure, at runtime or compiler time, real avaiable memory.

I ask this because I have a problem when make a Sketch over 8KB (with Arduino UNO and IDE version 0022), after many tests I find that my upper static variable are located, into ram address 1858, and my dynamical variable (a byte inside my function, therefore located in the upper positions of the stack) have a ram address of 2275. Do you remember that Stacks, tipically, work decrementing the ram location pointer from the upper of the avaiable memory (2303). If I calculate difference 2275-1858 I have 417 free ram address before to have a collision of the stack with static variable, but in thi condition if I add a function (extend stack for call the function), or add two "int" inside an existing function, I have a abnormal response at runtime.

To comprove this phenomena, I have ottimizated the code remoning local variable inside variuos existing function, and with this optimization I have extended the Sketch code until 10KB without runtime problems. At this point I have 420 bytes free, too; but now I need to add a small piece of code, calling a new function, and Now it appeare impossible. Note that I have 20KB free of rom but in the reality it is impossible to use.

There is someone that can help me, please? Thanks

Don't forget that RAM and program memory are completely different. Do you have large constant arrays or tables?

Don't forget that RAM and program memory are completely different. Do you have large constant arrays or tables?

No, I have only a small constant array (about 40bytes), but I have many constant string for debugging by serial.print. How I saw before the problem appear to be the RAM, not the ROM (flash).

Oddly, the problem is appeared after that I wrote more than 8KB of flash program; I suppose than, after this flash size, the compiler manages in different mode the function calls, and this, for some reason that I don't understand, increases memory usage.

but I have many constant string for debugging by serial.print.

The smoking gun.

The smoking gun

. Please, could you explain?

I don't know how many print strings you've got (hint) but each one occupies RAM.

I don't know how many print strings you've got (hint) but each one occupies RAM.

I thought that serial.print used the ROM and not RAM, but probably you are right, this would explain many things.

Now I try to write more short messages into serial.print. thanks.

I thought that serial.print used the ROM and not RAM, but probably you are right,

Serial.print uses char arrays. That's all it knows about. It can't know if they're in ROM or RAM, so just to be sure, it always puts them in RAM.

It can't know if they're in ROM or RAM, so just to be sure, it always puts them in RAM.

AWOL you are right. with this simple function...

DebugMessage(const char *str ){ Serial.print(str); Serial.println((int)str, DEC); }

I have traced the location of the constant string, and they are in low zone of the ram (from address 470 to 1860).

However, I don't understand how it is possible to have problems with some 400byte ram free, leaved from the higher static (1900) to the dynamic lower (2200). Can someone explain? Does anybody know which function, or system, use those ram locations (between 1900 and 2200)?

Can someone explain?

Without seeing your code? No, I seriously doubt it.

thank AWOL, Anyway thank you very much, your help was very helpful to me. Now I'm shortening all the messages in order to save a ton of bytes

You can put strings into PROGMEM, but you need to handle the printing yourself, but it is fairly simple.

DarioCortese: Now I'm shortening all the messages in order to save a ton of bytes

This will help... http://arduiniana.org/libraries/flash/

Thank Coding Badly, very usefull.

You can put strings into PROGMEM, but you need to handle the printing yourself, but it is fairly simple.

Please can you post to me some examples, or some link? Thanks

#include <avr/pgmspace.h>
#define fp(string) flashprint(PSTR(string))
/*
 * flashprint
 * print a text string direct from flash memory to Serial
 */
void flashprint (const char p[])
{
    byte c;
    while (0 != (c = pgm_read_byte(p++))) {
	Serial.write(c);
    }
}

  :

   fp("\nThis is a debug message in flash");

Amount of free memory..

Try this.. I have some simple instructions in the program.

Put the function at the end of your code.

Call it from the end of setup.

Probably the best place for the extern variables is near the top of the sketch. You'll figure it out...

Put the two extern variables near the top of your program -- call the memory free routine from the end of setup for example...

/*
  Free Memory detection
 From Arduino Cookbook
 Modified by WillR March 22, 2011
 Did I improve it or change it?
 Good Question!

 */

// external variables from build process

//String mystring[] = "testing";

void setup() {

  Serial.begin(9600); 

} //end setup

void  loop()
{
  Serial.print("Free Memory:  ");
  Serial.print(memoryFree()); 
  Serial.println("  ");  
  delay(1000);

} //end loop



extern unsigned long __bss_end; // put this at top of your sketch
extern void *__brkval;   // put this at top of your sketch         

int memoryFree()     // In YOUR Program -- call this from the end of setup and wherever else you wish
{
  //  long myValue;
  int freeValue;
  freeValue = 0;

  if ((unsigned long)__brkval == 0)
  { 
    freeValue = ((unsigned long)&freeValue) - ((unsigned long)&__bss_end);
  }
  else
  { 
    freeValue = ((unsigned long)&freeValue) - ((unsigned long)__brkval);
  }

  return freeValue;

}//end memoryFree()

Cheers! ]:D

Hope that helps!