Solved

Threads

Posted on 2004-08-17
20
1,872 Views
Last Modified: 2008-03-17
Hi folks,

I would like to know if there is a specific limit on the maximum number of threads that a process can have (preferably for Windows OS). I know that in .NET, the Thread-pool object can have a max of 25, but does that mean that there can be only 25 threads per process in the OS?

Also, is there a way to increase the number of threads per process (using .NET)?

What is the impact of increasing the number of threads per process (in terms of performance, memory, CPU utilization, etc) - I hope that this last question can be answered irrespective of OS and the programming language being used.

Thanks,
Mayank.
0
Comment
Question by:mayankeagle
  • 6
  • 4
  • 3
  • +5
20 Comments
 
LVL 30

Author Comment

by:mayankeagle
ID: 11818326
>> I hope that this last question can be answered irrespective of OS and the programming language being used.

Would like to have inputs from all experts (Java/ .NET, etc) on that.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11818331
There is, especially on a Unix system (where they're processes) but finding out about a closed OS like Windows could be tricky. Maybe better to ask in the Windows TAs ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11818365
>>. Maybe better to ask in the Windows TAs ...

It's better to ask in other TAs because this is kernel level stuff, not application really
0
 

Assisted Solution

by:spetzer
spetzer earned 50 total points
ID: 11818381
Hi.

Unforunately .Net doesn't allow you to increase the number of threads, or atleast not that I know of. Coming back to the operating system... The operating system doesn't have a limit, it is more based on your computer hardware. Keep in mind that a thread is an instance or a part of your application running on its own. If you run more threads, your cpu usage will increase as well as you memory usage, which will have a negative impact on the overall performance of your computer. To view the amount of threads running, open your task manager, click on the Processes tab, Click on view then click on Select Columns, then make sure Thread Count is ticked. Click OK and then you'll see that a new column as been added showing each process' number of threads.

I hope that this will help.

Regards
Stefan
0
 
LVL 30

Author Comment

by:mayankeagle
ID: 11818404
Thanks for your inputs, guys. Have also posted links on the other TAs.

>>  your cpu usage will increase as well as you memory usage, which will have a negative impact on the overall performance of your computer

That's what I was looking at. Just wanted a clarification.

>> .Net doesn't allow you to increase the number of threads

I think that at least the ThreadPool class does. But am not sure if that increases just the maximum number of threads that you can obtain from the thread-pool or whether it also increases the number of threads that you can create/ instantiate using the Thread class's constructor.
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 11818435
Maximum number of threads in the thread pool doesn't say anything about maximum allowed number of threads per process. Number of threads per process is much more than 25, I think it is restricted only by available memory.
Thread pool is used to reduce number of thread switches. For example, you write server application and create thread for each client. When you have 100-200 clients, server program spends most of it's time to thread switching and not to actual work. Optimal number of threads in the thread pool is usually number of processes * 2. 25 is enough for thread pool, but this doesn't restrict number of non-pool threads.
0
 
LVL 30

Author Comment

by:mayankeagle
ID: 11818476
>> There is, especially on a Unix system

How much :-) ?
0
 
LVL 30

Author Comment

by:mayankeagle
ID: 11818484
>> but this doesn't restrict number of non-pool threads.

Oh. I also wanted to know the limits on non-pool threads, if any. Is there a documentation from Microsoft or any other such OS vendor on this? I couldn't find on MSDN (except the thread-pool).
0
 
LVL 3

Assisted Solution

by:Tyrsis
Tyrsis earned 175 total points
ID: 11818525
There is a definate maximum on windows machines for threads.  It's not based on total memory, but is based around Virtual Memory and how Virtual Memory is doled out.  From my experience, you're not going to have much more than 2000 threads in total (All processes running).  It may be a bit less or a bit more than this, I can't remember the exact number, but this number is independant from your machine or how much memory you actually have.  

In terms of performance, with modern day machines, context switching is almost unnoticeable, even with a large number of threads.  While there is a tiny bit of overhead for creating threads, it really isn't memory/cpu intensive at all to just create a thread that doesn't do anything.  Debugging many threads on the other hand, can be a nightmare if you don't synchronize well.  To be honest, I've used both methods of pooling and just throwing as many threads at the problem as I can, and both methods have their good and bad points.  

Tyrsis

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 11818694
>>>>
>> There is, especially on a Unix system

How much :-) ?
>>>>

Sorry about the 'especially' - that had the wrong implications. What i *meant* to imply is that these mysteries are often less mysterious on a Unix platform, but unfortunately, because of the multiple 'flavours' you should examine the issue in light of a particular Unix. e.g.

http://www.uwsg.iu.edu/UAU/system/sysinfo.html


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Assisted Solution

by:jdeclue
jdeclue earned 75 total points
ID: 11819435
Most applications running on the Windows 2000 Operating system will typically begin to degrade when running with over 2000 open objects or threads, whether these are open files, memory locations, functions, registry keys etc... Depending on the processor and memory this can go up or down, but not by a lot, usually. Processor and Memory will often let you run your processes and threads faster, but not you usually do not gain a whole lot in the amount you can open.

   I have found that typically when there are more than 2000 open objects being handled by the processes of an applicaiton, you usually run into the processes of the OS and start thrashing the OS. Of course this is all dependant upon the Application itself and other processes running on the server. So I agree with Tyrsis on the 2000 number.

A great way to look at your system and see what other apps and the OS is doing is by download Process Explorer from SysInternals. http://www.sysinternals.com/ntw2k/utilities.shtml , it is a free app. No developer or sys admin whould be without it.

J
0
 
LVL 22

Assisted Solution

by:cookre
cookre earned 100 total points
ID: 11821645
Here's someone who ran max thread counts on various OSs:
http://gregluck.com/blog/space/Establishing+the+Maximum+Number+of+Threads
0
 
LVL 3

Accepted Solution

by:
Tyrsis earned 175 total points
ID: 11824072
Ah yes now I remember.  

In all previous versions of windows NT (NT4 / 2000), maybe not XP, but I'm not sure if they actually changed it or fixed this yet.  But Processes on the OS can only reserve up to 2GB of Virtual Memory.  Everytime you create a thread, the system reserves a MINIMUM of 1MB per thread for it's stack.  So basically 2GB / 1MB = 2048 max threads on the OS.  You could not shrink the size of the stack space for a thread either, the minimum is 1MB.  Now what I'm not sure about is if in XP the maximum Virtual Memory size was increased.  

Also, versions of windows based on the 9x kernel I believe had lower thread limits.  I can't remember because it's been awhile since I've developed on these systems.  

The above link given, I read it quickly, it shows that the JVM can have a stack size of 256k on windows XP?  I know this isn't possible using the win32 API.  And from the looks of it, he didn't actually create the threads, just divided the Virtual Memory size by the stack size, which would work if it allowed you to shrink the stack size smaller than a MB, which CreateThread doesn't allow you to do (You can only increase it, and it's rounded to the nearest MB).  

Tyrsis
0
 
LVL 9

Expert Comment

by:jdeclue
ID: 11824142
Bingo... Tyrsis... what a memory jogger... 2GB limit for a process.. that is exactly right. I knew there was a reason it didn't have to do with more processor and memory.

J
0
 
LVL 30

Author Comment

by:mayankeagle
ID: 11827628
Thanks, guys. I guess those are enough inputs.
0
 
LVL 30

Author Comment

by:mayankeagle
ID: 11827633
I tried with .NET on Windows 2003 - didn't allow me to create more than 1871 threads (OutOfMemoryError after that). I maybe having some other processes running (like Outlook and some background processes like Anti-virus). But the reports on the link that cookre posted show that there were 7200 threads on Windows XP. Will try it out too.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11829203
8-)
0
 

Expert Comment

by:dguribe
ID: 11858066
I guess it will be about 1800
But it depends on the amount of memory
0
 
LVL 3

Expert Comment

by:Tyrsis
ID: 11858077
No, it doesn't depend on the amount of memory.  You can not have more than 2048 threads.  It depends on virtual memory.  Everytime you create a thread you reserve 1MB of virtual memory.  And XP / etc can only use 2GB total of reserved virtual memory.  So 2GB / 1MB = 2048 threads.  This is an OS limit.  If someone with more than 2GB of memory can tell me differently, then please do, but I'm pretty sure this is how it works, at least for now.  

Tyrsis
0
 
LVL 9

Expert Comment

by:jdeclue
ID: 11869434
You can have 2048 threads per 2BG of memory and you have swap that can increase the value. THe limitation applies to the threads running under a single process. So if I have 3 apps open, I could have 3 x 2048 threads. But that is under absolute ideal conditions, in which my threads are doing anything with memory. If each of those threads require more than 1 mb of memory, then it starts dropping. In addition the OS has a lot of processes and uses threads etc.... it goes on and on.

If I were to develop an application for Windows .NET that required a ton of processing and threads, I would write it so I could start multiple processess with a limited number of threads. This way I do not have to worry about thread count. Instead I would start up multiple processes, if the machine could handle 10, then I would start 10 processes, if I add processors and memory then I would start more. In this fashion, I am never concerned with threads etc. I just start more processes.

J
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now