Maximum number of threads in a process

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

Is there an operating system limit to the number of threads in a process ? A
test program I was writing created approximately 105 threads for a process
( many unused because of some thread pool code ), and an out of memory error
occurred when an attempt was made to create more threads. Since I do not
believe that the application was using much memory compared to the nearly 1
GIG of total virtual memory, I assume that the out of memory exception had
to do with exceeding a thread creation limit for a single process.
 
Edward Diener said:
Is there an operating system limit to the number of threads in a process ?

No. The limiting factor usually turns out to be memory. Every thread by
_default_ gets a 1MB stack. Sooner or later you run out of contiguous
virtual address space.

I am surprised though that you hit the wall at 105. I have seen native
programs can several hundred more threads before giving up the ghost. I
haven't seen any reports about likely limits under .Net

<aside>
That said, where high performance is required, it is rarely a good idea to
have so many threads if all of them can compete for the processor. On the
native side of things, a thread pool and an I/O completion port scale
better.
</aside>

Regards,
Will
 
You can set a smaller stack size for your threads maybe that helps.
But in general if you have lots of threads thread pooling is the best
solution.
 
William DePalo said:
No. The limiting factor usually turns out to be memory. Every thread by
_default_ gets a 1MB stack. Sooner or later you run out of contiguous
virtual address space.

I am surprised though that you hit the wall at 105. I have seen native
programs can several hundred more threads before giving up the ghost. I
haven't seen any reports about likely limits under .Net

I realized that the number of threads created was closer to 300 rather than
100. Each thread pool was creating 25 threads. I have since corrected the
test program I am running to use a single thread pool rather than the dozen
or so I was previously using, and everything runs fine now. Thanks for your
explanation about each thread using a 1MB stack. That would explain why I
ran out of memory.
<aside>
That said, where high performance is required, it is rarely a good idea to
have so many threads if all of them can compete for the processor. On the
native side of things, a thread pool and an I/O completion port scale
better.
</aside>

There was a thread pool but it was encapsulated in a class which I was
instantiating numerous times even though I really only needed one instance
of the class.
 
Edward,

Are you aware that .NET has a built-in thread pool available. You made
it sound as if you've created your own and that maybe fine if there are
special requirements. I just thought I'd give you a friendly heads-up
in case you were not aware of it.

Brian
 
Brian Gideon said:
Edward,

Are you aware that .NET has a built-in thread pool available. You made
it sound as if you've created your own and that maybe fine if there are
special requirements. I just thought I'd give you a friendly heads-up
in case you were not aware of it.

Yes, I was aware of it and the decision to use another thread pool was not
mine. Thanks, nonetheless for the heads-up.
 
cody said:
You can set a smaller stack size for your threads maybe that helps.
But in general if you have lots of threads thread pooling is the best
solution.

Thanks for the info about setting a smaller stack size. I will look into
this.
 
Back
Top