Free RAM on Teensy 4.1

gonzales

Well-known member
Hi!
Is it possible to find out how much free RAM (RAM1 and RAM2) is left on Teensy 4.1

My board sometimes unexpectedly reboot, I want to understand why. Maybe I have a memory leak.

My memory usage now

FLASH: code:699008, data:153072, headers:8560 free for files:7265824
RAM1: variables:129060, code:323016, padding:4664 free for local variables:67548
RAM2: variables:12448 free for malloc/new:511840
 
Hi!
Is it possible to find out how much free RAM (RAM1 and RAM2) is left on Teensy 4.1

My board sometimes unexpectedly reboot, I want to understand why. Maybe I have a memory leak.

My memory usage now

FLASH: code:699008, data:153072, headers:8560 free for files:7265824
RAM1: variables:129060, code:323016, padding:4664 free for local variables:67548
RAM2: variables:12448 free for malloc/new:511840
Make your setup something like:-
Code:
void setup() {
  Serial.begin(9600);
  /* check for CrashReport stored from previous run */
  if (CrashReport) {
    /* print info (hope Serial Monitor windows is open) */
    Serial.print(CrashReport);
  }
}
See here for explanation.
 
There have been a few different sketches that have some form of memory checking, to look maybe at things like:
how big the heap might be getting. Note: it does not actually go through the heap, it may only look at how far up has the
low level memory allocator has grown to... Also some that try to figure out how far down the stack may have gone during the run.
This is done by at startup trying to fill all unused memory (between heap and stack) with some know value and then when requested
walk the memory to find where it is not still that value...

One sketch that is in the builds, that I know has done this is part of the ST7735_t3 library:

Note: sometimes when I have used this you have to tweak some stuff in the code, like how close you can go to some memory areas without it
faulting...
 
Make your setup something like:-
Code:
void setup() {
  Serial.begin(9600);
  /* check for CrashReport stored from previous run */
  if (CrashReport) {
    /* print info (hope Serial Monitor windows is open) */
    Serial.print(CrashReport);
  }
}
See here for explanation.
I didn't know about such a wonderful thing. Thanks. In any case, it's very useful.
One sketch that is in the builds, that I know has done this is part of the ST7735_t3 library:
Thanks a lot for the tip.

Went to study;)
 
Does this help?

I use the following in my app.
Code:
extern unsigned long _heap_start;
extern unsigned long _heap_end;
extern char *__brkval;

uint32_t freeRAMTeensy() {
    return (char *)&_heap_end - __brkval;
}

uint32_t freeStackTeensy() {
    extern char _ebss[];
    auto sp = (char*) __builtin_frame_address(0);
    const uint32_t stack = sp - _ebss;
    return stack;
}
 
Does this help?

I use the following in my app.
Code:
extern unsigned long _heap_start;
extern unsigned long _heap_end;
extern char *__brkval;

uint32_t freeRAMTeensy() {
    return (char *)&_heap_end - __brkval;
}

uint32_t freeStackTeensy() {
    extern char _ebss[];
    auto sp = (char*) __builtin_frame_address(0);
    const uint32_t stack = sp - _ebss;
    return stack;
}
thanks, i will try it
 
Back
Top