Memory issue / Determine remaining RAM

Hi,

I'm working on a code using big buffers, and I suspect a memory issue. Is there anyone who succeed to get the available RAM at compile time and/or at runtime ?

Thanks :slight_smile:

Try this...

------------

extern caddr_t data_start;
extern caddr_t data_end;
extern caddr_t bss_start;
extern caddr_t bss_end;

#define RAM_START_ADDR 0x20000000
#define RAM_SIZE 0x00008000

char buffer[256];
const char data[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *pbuff;

void setup() {
while( !SerialUSB ) ;
SerialUSB.begin(9600);
SerialUSB.print("Used data:");
SerialUSB.println( &data_end - &data_start );
SerialUSB.print("Used bss:");
SerialUSB.println( &bss_end - &bss_start );
SerialUSB.print("Free RAM (less stack):");
SerialUSB.println( RAM_SIZE - (&bss_end - &bss_start) );
}

void loop() {
// put your main code here, to run repeatedly:
}

check also - Arduino Playground - AvailableMemory

Rob : the code you posted doesn't seems to work on ARM...

Jsmith :

I tested your code with this program :

extern caddr_t __data_start__;
extern caddr_t __data_end__;
extern caddr_t __bss_start__;
extern caddr_t __bss_end__;

#define RAM_START_ADDR    0x20000000
#define RAM_SIZE          0x00008000


void setup() {

  while ( !SerialUSB ) ;
  SerialUSB.begin(9600);
  showRam();
  fact(50);
}

void loop() {
  // put your main code here, to run repeatedly:

}



void showRam() {
  SerialUSB.print("Used data:");
  SerialUSB.println( &__data_end__ - &__data_start__ );
  SerialUSB.print("Used bss:");
  SerialUSB.println( &__bss_end__ - &__bss_start__ );
  SerialUSB.print("Free RAM (less stack):");
  SerialUSB.println( RAM_SIZE - (&__bss_end__ - &__bss_start__) );
}

uint64_t fact(int i) {
  int time = millis();
  SerialUSB.print("i = ");
  SerialUSB.println(i);
  SerialUSB.print("Time = ");
  SerialUSB.println(time);
  showRam();
  SerialUSB.print("\n\n");
  delay(1000);

  if (i <= 0) {
    return 1;
  }
  return fact(i - 1);
}

But I always get the same result :

Used data:37
Used bss:496
Free RAM (less stack):32272

It will always return the same values based on what you have for declared variables, buffers, etc... IF you implement the _sbrk function so malloc will work, then the heap can be checked at runtime to see how much ram is free dynamically. the data segment and bss segments are provided by the linker and are NOT dynamic... This is an embedded system.

Q: Anyone using malloc & free ?

Jeff -

Ok - For a memory free test try the following code. Note that the slab allocator only grabs chunks when needed and that the "delete" does not seem to affect the free amount. I assume that with a more exhaustive test that the delete will be shown to work. Also note that YOU CANNOT MIX new/malloc and free/delete functionality.

--------

extern "C" char *sbrk(int i);

int FreeRam () {
char stack_dummy = 0;
return &stack_dummy - sbrk(0);
}

void setup() {
while( !SerialUSB ) ;
SerialUSB.begin(9600);

}

int n = 0;
char *buffer;

void loop() {
n++;
buffer = new char[n];
SerialUSB.print( "Free Ram :" + String(FreeRam(), DEC) + " After new of:" + String(n, DEC) + " bytes");
delay(50);
delete[] buffer;
SerialUSB.println( " Free Ram after deletes:" + String(FreeRam(), DEC) );
if( n > 10000 ) n = 0;
}