How to get size of RAM in code ?

Status
Not open for further replies.

Elmue

Well-known member
Hello

I write a sketch that should run without changes on different boards.
I need to know in code how much RAM is availabale on the board.

For all Arduino boards this is simple:

Code:
uint32_t RamSize = RAMEND - RAMSTART + 1;

But when compiling the sketch for Teensy I get:

error: 'RAMSTART' was not declared in this scope
error: 'RAMEND' was not declared in this scope

Is there any equivalent ?

P.D.
I don't want to know how much RAM is free.
I want to know the total RAM.
 
I'm not sure if there are linker symbols like RAMEND, RAMSTART in the AVR space, but for the 3.0/LC:
  • The Teensy 3.0 had 16k of read/write ram, 128k of read-only flash, and 2k of EEPROM;
  • The Teensy 3.1/3.2 has 64k of read/write ram, 256k of read-only flash, and 2k of EEPROM;
  • The Teensy LC has 8k of read/write ram, 62k of read-only flash, and 128 bytes of EEPROM.
 
freeram()

I'm not sure if this is helpful. But SDFat from Greiman has a function Freeram in the library. Might be worth looking into that...

Code:
#ifdef __arm__
extern "C" char* sbrk(int incr);
int SdFatUtil::FreeRam() {
  char top;
  return &top - reinterpret_cast<char*>(sbrk(0));
}
#else  // __arm__
extern char *__brkval;
extern char __bss_end;
/** Amount of free RAM
 * \return The number of free bytes.
 */
int SdFatUtil::FreeRam() {
  char top;
  return __brkval ? &top - __brkval : &top - &__bss_end;
}
#endif  // __arm
 
Hello Michael

I know that.
But I want to write a sketch that determines RAM size in code for all Arduino and Teensy boards.

Currently RAMSTART and RAMEND are not defined for Teensy 3 and Teensy LC boards,
while for Teensy 2.0 and Teensy++ 2.0 they are defined.

RAMSTART and RAMEND are not linker symbols.
They are defined in the CPU specific header files. (for example iom32u4.h for Teensy 2.0)


Hello akicroz8

No.
I want the total RAM size.
 
Last edited:
MCU's RAM size or do you want amount of free/used RAM at any point during run-time? (heap/stack)
 
perhaps something like this:

Code:
unsigned ramsize(void) {
  extern unsigned long _estack; 
  return (((int)&_estack - (int)&_VectorsRam) | 0xfff) + 1;
}

(tested with T3.2 only)

- but this depends on where the linker chooses to place _vectorsRam. It should be somewhere in section .dmabuffers, which is after .usbdescriptortable (if existent). the "| 0xfff" should - do it...

For Arduino & other boards, try to use #ifdef TEENSYDUINO #endif around that.
 
Last edited:
Status
Not open for further replies.
Back
Top