18

I've read here that :

In v2.0, 3.5, and 4.0, ASP.NET initializes the CLR ThreadPool with 100 threads per processor(core)

That is correct , I checked it (I have 8 core machine , so 8*100 = 800):

enter image description here

But then I saw this and this:

maxWorkerThreads — Configures the maximum number of worker threads to use for the process on a per-CPU basis.The range for this attribute is from 5 through 100. The default is 20.

Question

I don't see how the numbers fits in here :

The first paragraph states that I have max 100 threads per core ( the image prove it , I have 8 cores).

But the second paragraph states that the default maximum worker threads per core is 20. So if I have 8 cores then I must have 8*20 = 160 max threads. not 800.

Can someone please shed light?

Update:

I just found a way to get the key element value via c# code :

enter image description here

So now the number are fit in ,but still - MSDN say the default is 20 , not 100

enter image description here

And then they do mention 100 :

enter image description here

What is going on here?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • You are confusing ThreadPool threads and ASP.NET worker threads. And as a curiosity it's OK but for all practical applications: When you ask about the max number of threads you're already doing something wrong. The number of worker threads is more the domain of the Server admin than that of the developer. – H H Jun 07 '14 at 19:14
  • @HenkHolterman Aren't they're the same ? asp.net uses threadpool threads which are the worker threads – Royi Namir Jun 07 '14 at 20:10
  • Yes, the worker threads are a subset. Their ceiling can be a useful tool for server resource management. The Threadpool limit is an arbitrary high number that never should be reached. – H H Jun 07 '14 at 20:23
  • 3
    @HenkHolterman Can you explain it further more with a deeper explanations. – hackp0int Jun 08 '14 at 05:11
  • Good question, I've spend a day reading articles and trying to understand hot IIS works. Could it be that `maxWorkerThreads` setting default value for `autoConfig` depends on IIS version? When I run my app under IIS Express 8 the value is around 4K and when I run it under IIS 8.5 on windows 8.1 the value is around 32K. – Andrii Litvinov Jun 09 '14 at 21:00
  • Seems like my assumption is correct. I've tried to change `maxWorkerThreads` to value `20` and the `workerThreads` value on thread pool under IIS 8.5 has changed to `80`. – Andrii Litvinov Jun 09 '14 at 21:14

2 Answers2

10

I have looked at source code and have found that default value for MaxWorkerThreads is set to 100

private static readonly ConfigurationProperty _propMaxWorkerThreads = new ConfigurationProperty("maxWorkerThreads", typeof (int), (object) 100, (TypeConverter) null, (ConfigurationValidatorBase) new IntegerValidator(1, 2147483646), ConfigurationPropertyOptions.None);

This field is added to properties collection in static constructor

ProcessModelSection._properties.Add(ProcessModelSection._propMaxWorkerThreads);

In property definition they do set default value to 20

[IntegerValidator(MaxValue = 2147483646, MinValue = 1)]
[ConfigurationProperty("maxWorkerThreads", DefaultValue = 20)]
public int MaxWorkerThreads

But this obviously give no effect. Maybe it's some kind of legacy implementation. By the way it behaves this way only if autoConfig is set to false. When it's set to true I have 32K worker threads in my application. Probably this behavior depends on IIS version.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • So the docs are old updated via attribute and not via property ? – Royi Namir Jun 10 '14 at 06:37
  • Could be, I actually have no idea. – Andrii Litvinov Jun 10 '14 at 06:39
  • p.s this whole uqestion came from this [book chapter which I read](http://books.google.co.il/books?id=aIs8drBVdaoC&pg=PA167&lpg=PA167&dq=%22For+the+tests+below+to+work+as+I+describe,+use+Windows+Server+2008+or+2008+R2%22&source=bl&ots=BMtKvwsDCj&sig=eIXL7Q0CRpklPnU1Wt78hfcAHoY&hl=en&sa=X&ei=WaiWU6T4GOny7AaAr4HIDQ&redir_esc=y) i'll be glad if you have a look at the test he did there - which I don't understand : if each core has 100 threads to work , why does in the first 10 seconds - there were only 13 threads used ? why not more ? ( he did there synchronous operation which tie a thread) – Royi Namir Jun 10 '14 at 06:42
  • (continue)... but still - he says that threadpool create 2 more threads each second. but why create threads if asp.net already have 100 threads READY TO GO !!!! ? can u have alook at it ? – Royi Namir Jun 10 '14 at 06:43
  • 1
    Actually ASP.NET doesn't have 100 threads at start time. ThreadPood creates new threads when requested up to minWorkerThreads value. Then if there are still work to do and threads are not enough it will add new thread each half a second when needed. – Andrii Litvinov Jun 10 '14 at 06:48
  • no. asp.net does have 100 threads per core at start time. http://i.stack.imgur.com/RvJQ3.jpg ( this guy build iis core) – Royi Namir Jun 10 '14 at 06:52
  • Try to check yourself by calling `ThreadPool.GetMaxThreads` and `ThreadPool.GetAvailableThreads` at application start time or when request is executed. – Andrii Litvinov Jun 10 '14 at 06:54
  • My friend , I already have. ive been working on it for the last 5 days. look here and see how initializations threads are diffrent and how asp.net does set 100 threads per core http://i.stack.imgur.com/vbQhD.jpg – Royi Namir Jun 10 '14 at 06:56
  • 2
    `ThreadPool.GetMaxThreads` doesn't give you number of threads created in thread pool. It's a number of threads that it is possible to create. Try check you app memory footprint. Each allocated thread takes about 1MB in memory. I don't think you app consumes 1GB at start time. – Andrii Litvinov Jun 10 '14 at 07:00
  • if so , if i have 200 users who requests data from the server , they will wait a long time till threadpool will have enough threads( assuming prv threads still working).... – Royi Namir Jun 10 '14 at 07:31
  • Exactly. You can set `minWorkerThread` to 25 for your 8 cores server if you know for sure that it will use it. But it is not recommended because if the load will fall down thread will still be in use but not that effective. Try read [this](http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx) article too. – Andrii Litvinov Jun 10 '14 at 07:44
  • Thank you my friend. my mistake was missing the "max" word. I thout they are initially initialized to 100 threads. appraently they initialized to MAX 100 ( per core). however i still wonder how iis (8 core) takes care of many many simultainously requests ( ~2 sec each) while growing its thread pool threads by 2 per second..... – Royi Namir Jun 10 '14 at 07:49
  • Sure, I send good time digging IIS and understanding how it works. I think for that many requests IIS maintains other queue. AFAIR this was described in those two articles from Thomas Marquardt. (One you've mentioned in you question). – Andrii Litvinov Jun 10 '14 at 07:59
0

According to the MSDN,

the default maximum [number of threads in the ASP.net server pool] for .NET 4.5 is 5,000

Source

cjcurrie
  • 624
  • 1
  • 4
  • 15
  • 2
    please read it again. request != thread. asp.net sets 100 threads per core. this attribute is for async operations – Royi Namir Jun 09 '14 at 07:58
  • @RoyiNamir, perhaps I misunderstand. .NET 4 in IIS 7 has 5000 max concurrent application pool threads per CPU, as reported on [this page of the MSDN](http://msdn.microsoft.com/en-us/library/dd560842%28VS.100%29.aspx) – cjcurrie Jun 09 '14 at 16:06
  • 7
    5000 max concurrent requsts ! not threads ! Again , thread != request. this is for async operations where a request doesnt tie a thread. (_The difference only matters when the requests are asynchronous (the request either has an asynchronous handler or a module in the pipeline completes asynchronously)._ read this excat line here http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx – Royi Namir Jun 09 '14 at 16:32