In an old whitepaper, I read that requested memory is never returned to the OS. I'm working with an application that spawns a lot of short-lived threads every minute (with little memory used per thread), and was wondering what happened to its memory upon thread-destruction.
Under Windows, a user mode application (i.e. not devce driver) has a main thread, and optionally threads spawned from the main thread. All these threads share the same Virtual Address space and share a common heap(s). IOW a process(application) private heap.Therefore thread termination does not return memory to the process private heap. When the main thread returns (terminates), the process exits (ExitProcess) and all threads spawned by the main thread (if running) are terminated. When the process terminates, the entire virtual address space and RAM and/or Page File space it occupies is returned (this may be a lazy return).
If you have a main thread that continuously
loop: spawns threads these threads run and allocate memory spawned threads terminate without returning allocations goto loop
Then you will eventually run out of heap (for that app) and/or Page File space (for all apps).
The memory allocator doesn't know that a dead thread hasn't handed out
an allocation to another thread still alive, so it must assume that all
allocations are still live, which means that in a long-lived application
a thread should clean up after itself before terminating. If they do
that, total memory consumption will not rise unboundedly above the
high-water mark (there's overhead), because the orphaned private memory from the dead threads is brought together and reused before the allocator goes out and gets more O.S. memory, which I think sufficiently addresses your inquiry.
Whether memory is returned "to the O.S.", if any attempts to do that have been added to TBB recently (I don't know?), also depends on all allocations from those big chunks having been released by the application, and statistically you must have freed a lot of allocations to get significant odds that many big chunks are now completely free, so you probably cannot count on that. But that's outside the original question, I believe.