Last I checked, the kernel allocates a bunch of address space per thread but it doesn't consume actual memory unless you touch it and page fault.
Tying your memory consumption heavily to the max stack depth also seems like it would be difficult to reason about IMO.
- Arcing everything
- using a fixed-sized ancillary array and passing indices
- creating threads that correspond to the lifetimes you care about
- fighting your way through adding explicit lifetime allocations and
- delaying reclamation of _everything_ above it on the stack
...
and while workable, none of those things are particularly niceThat’s mapped space, so depending on the OS it could be more or less friendly to its memory subsystem. Linux won’t bother finding you actual resident pages until you do need them. But if the stack drops back down, it will be swapped out as any other dirty memory. Rust does not do any MADV_DONTNEED magic behind your back to aid with reclaiming memory.
One common thought I see a lot though is that each thread uses 1MB (or more) in stack space alone. This just isn't true with modern memory paging