开发者

Is there hard limit to the number of threads that can exist in a .NET process? [duplicate]

开发者 https://www.devze.com 2022-12-18 10:07 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Maximum number of threads in a .NET app?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Maximum number of threads in a .NET app?

Is there a limit on the number of threads we can create in a .NET application ?

I am assuming that the number of threads that can be created is limited by the amount of memory available since the threads' stack needs to be allocated. Please correct me if I am wrong. Are there other facto开发者_JS百科rs that limits the number of threads ? Or, is the number of threads limited to a specific number ?

How can I (roughly) calculate the maximum number of threads that can be created on a machine, if I know the machine specifications ?


As always, Raymond Chen has the answer on his blog. Note that his test appears to have been run using unmanaged code. My guess is that there's nothing in the .NET framework that actually limits the number of threads per process and that the limit would be enforced by the O/S. If that's truly the case then his test is still valid.

Also, I'm not sure if it's different between 32-bit and 64-bit machines, I would imagine his results are dependent on RAM size and 32bit/64bit CPU along with possibly the number of CPUs. All that said, it looks like he was able to get 13000 threads created.

The big issue with 13k threads running is that the time spent context switching is sure to eat up all the available cpu and you're likely to be getting little to no work done.

If the application you're looking into is creating a lot of threads that are supposed to be doing intense work, you might not be getting a process hang as much as running into issues with the amount of context switching taking place. Obviously the most common issue in a multi-threaded application is a resource deadlock, but there are many tools available to troubleshoot that scenario.

Try the following links about deadlocks to determine if that's what you're actually running into:

  • Avoiding and Detecting Deadlocks
  • Deadlock Monitor
  • Stack Overflow Deadlock Question
  • Stack Overflow Deadlock Debug Question
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号