Teensy 4.0 memory allocation

Status
Not open for further replies.

ossi

Well-known member
I want to get the amount of free memory and the actual values of the stackpointer and heap-allocator.
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(){
  }
The result is the following:
Code:
start teensy40freeRam1...
stackTop   =20077FE4H
heapTop    =20200008H
&staticVar =20000E2CH
FreeMem =FFE77FDCH
end setup...
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?
 
Give this a read and it will help see memory allocation for Teensy 4: pjrc.com/store/teensy40.html

Also this thread covered some of that and other along the way :: T4-0-Memory-trying-to-make-sense-of-the-different-regions

That thread shows an exe that generates output like this showing the heap and stack each come from a unique 512KB section of T_4.0 RAM:
Code:
FlexRAM section ITCM+DTCM = 512 KB
    Config : aaaaaaab
    ITCM :  25248 B	(77.05% of   32 KB)
    DTCM :  12992 B	( 2.64% of  480 KB)
    Available for Stack: 478528
OCRAM: 512KB
    DMAMEM:  12384 B	( 2.36% of  512 KB)
    Available for Heap: 511904 B	(97.64% of  512 KB)
Flash:  35776 B	( 1.76% of 1984 KB)
 
How is allocation done on Teensy 4.0 and how can i get the amount of free memory?

Here's the memory diagram from the Teensy 4.0 product page

teensy4_memory.png


That code is written for the memory map used on Teensy 3.2, where the stack starts at the end of the RAM and grows downward, and the heap starts near the beginning of RAM (just after static variables) and grows upward.

On Teensy 4.0 we have 2 separate RAM banks. The stack grows downward from the top of RAM1, and the heap grows upward within RAM2. RAM2 always ends at 0x20280000. So you should be able to modify that code for Teensy 4.0 with something simple like:

Code:
  return 0x20280000 - heapTop;

However, this code wasn't very well designed from the beginning. It calls malloc() for a small 1 byte allocation and assumes malloc will return a pointer to the end of the heap. That's a pretty terrible assumption. If you've previously called malloc and free in ways that left unused fragments in the heap, malloc could potentially give you a pointer to anywhere it finds an unused hole between existing allocations.
 
??? Oh dear.
If I am loading SD files into memory, some of which may be > 80K and want to be able unload/load a different file, what can I do? Do folks write their own heap management code, or do folks malloc() fixed-size blocks of memory and re-use them? The latter seems sane, but it creates conservative upper limits on file size.
 
Status
Not open for further replies.
Back
Top