I was experimenting with thread creation in a managed process, to test the limits, and I noticed something very odd. Creating a large number of threads was using a surprisingly large amount of memory. I guessed it must be the thread stacks, but the numbers didn’t fit with my understanding of how thread stacks are allocated.
In an unmanaged process, by default each thread has 1 Mbyte of address space reserved for its stack, but initially only a small amount of this address space is committed. You can see this with the excellent VMMAP utility – here’s what it shows for an unmanaged x64 application:
The ‘size’ is the reserved address space – 1024k as expected. The committed memory is 16k, which is the amount of the address space that actually has physical storage associated with it. This can grow as required, up to the size of the reserved address space, but usually it won’t get close to that.
The size of the reserved address space is important in the sense that we need to make sure we don’t run out – 2000 threads would be enough to max out a 32-bit process, for example. But generally speaking it’s the committed size that has more impact – that number has to be backed by real storage, in RAM or at least the page file.
So, back to the managed process. This is what VMMAP shows:
Reserved address space is the same as before at 1024k, but the whole thing is committed! So each thread is using the full 1 Mbyte of physical storage, regardless of how much memory the stack actually needs. That can’t be right, can it? Well it seems it is, as Joe Duffy explains.
So, the bottom line is that you should think carefully about creating managed threads that are going to sit idle in your process, for example in a thread pool. In an unmanaged process those idle threads wouldn’t be using many resources, but in a managed process they’re using a significant chunk of valuable memory. This is even more important if you have any form of multi-process architecture, perhaps with thread pools in each process.
If you really need a large number of long-lived threads in your managed process, consider reducing the size of the stack using the appropriate overload of the System.Threading.Thread constructor.