Arduino Forum

Products => Arduino Zero => Topic started by: AloyseTech on Dec 15, 2015, 01:42 am

Title: Memory issue / Determine remaining RAM
Post by: AloyseTech on Dec 15, 2015, 01:42 am
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 :)
Title: Re: Memory issue / Determine remaining RAM
Post by: jsmith on Dec 30, 2015, 07:29 am
Try this...

-----<SNIP>-------


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:
}
Title: Re: Memory issue / Determine remaining RAM
Post by: robtillaart on Dec 30, 2015, 02:52 pm
check also - http://playground.arduino.cc/Code/AvailableMemory
Title: Re: Memory issue / Determine remaining RAM
Post by: AloyseTech on Dec 30, 2015, 03:01 pm
Rob : the code you posted doesn't seems to work on ARM...

Jsmith :

I tested your code with this program :

Code: [Select]
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
Title: Re: Memory issue / Determine remaining RAM
Post by: jsmith on Dec 30, 2015, 08:00 pm
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 -
Title: Re: Memory issue / Determine remaining RAM
Post by: jsmith on Dec 31, 2015, 12:43 am
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.

----<SNIP>----

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;
}