Does anyone know? And a bigger question is what happens when you encounter this maximum? Is this the same number with other Windows OSs such as Vista, XP etc.?
-
Im guessing its not the number of threads, but the memory usage that is the limiting factor. – zsharp Jan 27 '09 at 00:21
8 Answers
First I would advise reading this: http://blogs.msdn.com/oldnewthing/archive/2007/03/01/1775759.aspx
then http://blogs.msdn.com/oldnewthing/archive/2005/07/29/444912.aspx
To summarise, the limitation is normally stack space (which must be in contiguous blocks) and since every thread consumes this scattered about you rapidly run out of contiguous blocks. On 64 bit machines and operating systems this is much less of a problem.
Mitigation strategies exist but will only go so far (and rely on you not using much stack per thread)
As a rough guide:
- creating tens is almost certain to work
- hundreds is probable on current server and desktop hardware but risky
- thousands will almost certainly fail.
You likely shouldn't need to create more than ten anyway (and if you really do need to you should know this information already)

- 36,004
- 6
- 77
- 101
-
Creating them all early and pool them for later use when the memory is not fragmented should also help. – Marco van de Voort Aug 24 '11 at 07:38
-
https://blogs.msdn.microsoft.com/tmarq/2007/07/20/asp-net-thread-usage-on-iis-7-5-iis-7-0-and-iis-6-0/ – hod caspi Dec 25 '18 at 16:11
The best answer I've heard when asking such questions is:
It doesn't matter, and if you find that it does matter, you need to rethink what you're doing so that it doesn't matter.

- 142,167
- 33
- 283
- 313
-
6Bravo, threads become self-defeating (and inefficient) when there's hundreds of the little buggers floating around. Use a thread pool and you won't have to worry. – paxdiablo Jan 27 '09 at 00:29
-
1+1 for the comment. Made my day. And when it comes to threading, it certainly holds true. – Kornelije Petak Aug 07 '11 at 12:22
-
I'm going to run Windows on Knights Landing. So it now does very much matter with 256-288 threads – user1649948 Jun 20 '16 at 22:17
Note that you should examine your design closely if you are concerned about hitting this limit!!!!!!!!
The answer to your "More Important Question" of what happens is OutOfMemoryException.
Not exactly a direct answer, but here's some code to find out the limit. It could be available memory dependent though. Would be interested in seeing other OS/cpu/mem results.
Feel free to edit and add your machine in:
Windows 7, VS2008, dual core, 2gb mem: 1,465 then crash with OutOfMemoryException
int i = 0; try { while (true) { new Thread(new ThreadStart(() => Thread.Sleep(int.MaxValue))).Start(); i++; } } catch (Exception ex) { Console.WriteLine(i); Console.WriteLine(ex.ToString()); }

- 34,421
- 21
- 109
- 151
-
1+1 on the "if you have to ask then you're doing something wrong" sentiment! – Greg Beech Jan 27 '09 at 00:28
-
Why would you community wiki this? It's a good answer (I +1ed you); you should get credit for it. – John Rudy Jan 27 '09 at 01:11
-
Community wiki because I don't care about the rep stuff, and it makes it easier for others to add their results. – TheSoftwareJedi Jan 27 '09 at 01:21
Default stack size is 1MB and the user-mode address space assigned to the windows process under 32 bit Windows OS is about 2 GB. that allow around 2000 thread per process (2000 * 1MB = 2GB). for 64 bit, practically, there is no such a problem .

- 47,454
- 15
- 134
- 158
Do read the Raymond Chen blog postings that ShuggyCoUk's answer pointed to.
But pay special attention to this bit:
But the real question that is raised whenever somebody asks, "What's the maximum number of threads that a process can create?" is "Why are you creating so many threads that this even becomes an issue?"
The "one thread per client" model is well-known not to scale beyond a dozen clients or so. If you're going to be handling more than that many clients simultaneously, you should move to a model where instead of dedicating a thread to a client, you instead allocate an object. (Someday I'll muse on the duality between threads and objects.) Windows provides I/O completion ports and a thread pool to help you convert from a thread-based model to a work-item-based model.

- 1
- 1

- 333,147
- 50
- 533
- 760
Question seems very old but like to add as can be helpfull to others as well :
This article regarding : Pushing the Limits of Windows: Processes and Threads
http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx

- 275
- 5
- 16
As far as I understand the whole threading model it should not have changed much since Win2K.
There is no real limit of threads per se, but more a limit of the processes stack-space. See an in-depth explanation of threading limits from Raymond Chen for more details on this.

- 22,222
- 13
- 38
- 45
-
Bingo! "But the real question that is raised whenever somebody asks, "What's the maximum number of threads that a process can create?" is "Why are you creating so many threads that this even becomes an issue?"" – TheSoftwareJedi Jan 27 '09 at 00:27
If you are stuck with an existing design that utilizes a high number of threads and needs to scale, you might also consider fibers:
http://msdn.microsoft.com/en-us/library/ms682661%28v=vs.85%29.aspx
It can save you a total redesign.
Indy considered it for Indy 10, but it never happened because the .NET adventures consumed most of the time it seems.

- 25,628
- 5
- 56
- 89
-
ShuggyCoUk's second link to OldNewThing says the following about fibers: *Note that fibers do not help much here, because a fiber has a stack, and it is the address space required by the stack that is the limiting factor nearly all of the time.* – foraidt Apr 03 '12 at 12:32
-
ShuggyCoUk's link means it is not the ultimate solution. I see it more as a magnitude extra scaling where full threads won't go. But as said that knowledge is from dated discussions in a time where 64-bit meant alpha. – Marco van de Voort Jun 01 '12 at 22:32