Go Down

Topic: Serial.print issues (Read 743 times) previous topic - next topic

irethedo

Is there a way to tell how much sram is being used when code is uploaded to an arduino?

SurferTim

I include this function code to any sketch I want to check the SRAM.
Code: [Select]
int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 
}

Then call it something like this. I call it where I think the most memory is allocated, like in subroutines that have arrays, etc.
Code: [Select]
Serial.print(F("Free SRAM = "));
Serial.println(freeRam());

It does not show 0 when you run out of memory. It will show an unrealistic amount of SRAM remaining. If I run my Mega out of memory, it will return more free SRAM than my Mega has.

irethedo

Thanks Surfer Tim-

I tried this on my Uno code and it reads
Free SRAM = -1248

Does this mean I have 1248 bytes left?


SurferTim

That number should be positive, probably somewhere between about 1000 to 100, depending on the remaining SRAM. That would normally indicate you are out of SRAM.

Write a test sketch. I used something like this to get an idea of how much SRAM memory the device has when it starts with an empty sketch.
Code: [Select]
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("Free SRAM = "));
 Serial.println(freeRam());
}

void loop() {
}


irethedo

I tweaked a few things and got it up to 735 at one point in the code
but then at other points of the code it doesn't work (must be running out of memory at those areas I assume..)

I notice no difference removing constants but arrays seem to eat up some sram.

What types of commands/ data structures tend to chew this sram up so fast and what
should I look to change to free up some of this sram?

thanks

Go Up