Go Down

Topic: How much static RAM is used (Read 3853 times) previous topic - next topic

Joegi

Hi,
would be nice if the ARDUINO Application would show how much static RAM is used by a program!

CrossRoads

I think there's a freemem command you can use to find that out.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

stevemarple

I think this is the freeMemory function CrossRoads spoke about. I'm using it and it works fine. I got it from somewhere on this forum, but I don't remember who otherwise I'd give the credit they deserve.

Code: [Select]
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;
int freeMemory() {
  int free_memory;
 
  if((int)__brkval == 0)
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);
 
  return free_memory;
}

Joegi

...thank you for the code, but it would be nice if the ARDUINO application would show the amount of used/free static memory, as it shows the amout of used flash memory!

PaulS

Quote
but it would be nice if the ARDUINO application would show the amount of used/free static memory

It would be nice if pigs could fly, too. And about as likely.

There are compile time things that use SRAM and run time things that use SRAM. Reporting how much SRAM is going to be available at run time might be useful. But, for the compiler to tell you how much SRAM you are going to use at run time is an unrealistic expectation.
The art of getting good answers lies in asking good questions.

maniacbug

Its still a reasonable request for the ide to print what it knows, the bss and data sections.  At least you know where you're starting.

mromani

IIRC Microchip C compiler (and some compatible third party ones, like CCS's) give you a percentage of used sram memory. I never took the time to verify that value though...

fat16lib

The freeMemory program is useful to tell how much ram is left from the current top-of-stack to the heap.

It is nice to know what the "high water level" was for the stack.  This is traditionally done with small processors like the AVR by filling the space between top-of-stack and the heap with a known pattern and checking how much has been over written.

Here is a sketch I have been playing with that shows how to determine the "high water level" in a simple case.  This is not meant to be a "production quality utility", just a demo.

It prints freeRam and unusedStack before and after a call to a function that uses an array on the stack. 

Code: [Select]

extern int  __bss_end;
extern uint8_t* __brkval;
extern uint8_t __heap_start;
//------------------------------------------------------------------------------
void fillStack() {
  extern int __bss_end;
  extern uint8_t* __brkval;
  uint8_t* bgn = (uint8_t*)(__brkval ? __brkval : &__heap_start);
  uint8_t* end = (uint8_t*)&bgn - 10;
  while (bgn < end) *bgn++ = 0X55;
}
//------------------------------------------------------------------------------
uint16_t stackUnused() {
  uint8_t* bgn = (uint8_t*)(__brkval ? __brkval : &__heap_start);
  uint8_t* end = (uint8_t*)&bgn - 10;
  uint8_t* tmp = bgn;
  while (tmp < end && *tmp == 0X55) tmp++;
  return  tmp - bgn;
}
//------------------------------------------------------------------------------
int freeRam () {
//  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
//------------------------------------------------------------------------------
int useStack(int k) {
int b[256];
for (uint16_t i = 0; i < 256; i++) b[i] = i;
return b[k];
}
//-----------------------------------------------------------------------------
void setup() {
  fillStack();
  Serial.begin(9600);
  Serial.write("stackUnused: ");
  Serial.println(stackUnused());
  Serial.write("freeRam: ");
  Serial.println(freeRam());
 
#ifdef DEBUG_PRINT 
  Serial.write("bss end: ");
  Serial.println(reinterpret_cast<int>(&__bss_end));
  Serial.write("brkval: ");
  Serial.println(reinterpret_cast<int>(__brkval));
  Serial.write("heap start: ");
  Serial.println(reinterpret_cast<int>(&__heap_start));
#endif // DEBUG_PRINT
  Serial.write("Call useStack here\r\n");
  // use stack
  useStack(255);
  Serial.write("stackUnused: ");
  Serial.println(stackUnused());
  Serial.write("freeRam: ");
  Serial.println(freeRam());
}

void loop() {}

Go Up