I want to get the amount of free memory and the actual values of the stackpointer and heap-allocator.
I tried the following program:
The result is the following:
The heap seems to be above the stack. That is rather unusual. The amount of free memory computes thus is negative. How is allocation done on Teensy 4.0 and how can i get the amount of free memory?
I tried the following program:
Code:
int staticVar ;
uint32_t FreeMem(){
uint32_t stackTop;
uint32_t heapTop;
// current position of the stack.
stackTop = (uint32_t) &stackTop;
// current position of heap.
void* hTop = malloc(1);
heapTop = (uint32_t) hTop;
free(hTop);
Serial.printf("stackTop =%08XH\n",stackTop) ;
Serial.printf("heapTop =%08XH\n",heapTop) ;
Serial.printf("&staticVar =%08XH\n",&staticVar) ;
// The difference is (approximately) the free, available ram.
return stackTop - heapTop;
}
void setup() {
delay(1000) ;
Serial.println("start teensy40freeRam1...") ;
Serial.printf("FreeMem =%08XH\n",FreeMem()) ;
Serial.println("end setup...") ;
}
void loop(){
}
Code:
start teensy40freeRam1...
stackTop =20077FE4H
heapTop =20200008H
&staticVar =20000E2CH
FreeMem =FFE77FDCH
end setup...