Maximum stack size for Teensy 4.1

dgnuff

Active member
The output for a sketch build I'm working on looks something like this:

Code:
Memory Usage on Teensy 4.1:
  FLASH: code:358444, data:48696, headers:8600   free for files:7710724
   RAM1: variables:53952, code:279296, padding:15616   free for local variables:175424
   RAM2: variables:91424  free for malloc/new:432864

Assuming I don't put anything else in RAM1, do I have pretty much all of that 170K available for the stack, meaning I can allocate a 16K, or larger buffer if needed?

The reason I'm asking is that I got a very rude surprise on an ESP8266 a day or two ago when I discovered that it has a hard cap of 4K on the stack size, irrespective of the actual free memory.

I'd much rather place buffers of this sort on the stack, because they really are transitory, when the routine they're created in returns, they're gone, no longer needed. This is obviously the best use of memory, since some other routine can recycle that memory for its buffers.
 
Yes - Free for local variables is another way to say how much stack space you have...

edit: as for ESP32, is that the maximum stack size or the default? That is I believe when you create a task you can pass into it the size of the stack... But could easily be wrong!
 
Last edited:
I would say that's fine as long as the functions using the big local variables are part of the regular program flow / not called from interrupts, since interrupts can nest and you can't be sure how much stack you have left at those times.
 
edit: as for ESP32, is that the maximum stack size or the default? That is I believe when you create a task you can pass into it the size of the stack... But could easily be wrong!

Yeah, the 4k is the default stack for single threaded setup() / loop() code. I read a fairly sizeable thread on the ESP forums about this. They were also proposing a task as a way to overcome the default 4K stack limit. In my case, that would have been massive overkill, since I only have one routine that has a large buffer, there's enough ram available that I can simply convert the buffer to a static variable, and move it off the stack into BSS.

Anyway it's good to hear that the Teensy is not limited in the same way. Since RAM2 started getting really tight, I'm having a massive rewrite. I'd been using std::string a bit too liberally, and eventually wound up rebooting occasionally because a memory allocation failed. So I'm working now on something akin to C++17's std::string_view, which lets me take complete control of memory usage. That's why the need for routines to occasionally allocate a large buffer. If I can get those on the stack, it'll make a real difference.
 
Back
Top