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.
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.
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)?
#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");
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()