1

I have this code that shows the available sram memory

    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.println(freeRam());
}

void loop()
{
}

enter image description here

The arduino compiler shows a different value.

  • the arduino compiler shows 1856 bytes and
  • Serial port shows 1850 bytes

What is the correct value?

2
  • probably both ..
    – jsotola
    Apr 28, 2022 at 3:22
  • Did you take the used space on the stack into account? Apr 28, 2022 at 5:54

1 Answer 1

3

Both are correct depending on what value you are interested in. The compiler tells you the size of the global variables. Your code tells you how much memory is free at any one time. Running code requires memory (most often the stack growing to accommodate local variables and the "stack frame" with a copy of the CPU registers in it every time you call a function).

In other words the compiler tells you how much is in your glass when it's handed to you by the barman but your code tells you how much is in there once you have taken a sip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.