Virtual memory address and context switching overhead. On 32-bit platforms, if each thread has an 8 MB stack then after creating a few hundred threads you run out of VM address even if you don't actually use that much memory. Most OS schedulers also don't like dealing with tens of thousands of threads. Furthermore each kernel thread takes a small amount of space but kernel memory typically isn't swappable, unlike userspace memory.
Stack size I've discussed in another response. It can and should be decreased if you plan on working with a lot of threads.
I can't comment on the Mac OS X scheduler. The Linux scheduler handles it just fine, and my understanding is that the Windows one does OK with it too.
The small amount of kernel memory that isn't swappable likely isn't your system's overall throughput constraint, but if it is I stand corrected.
- 8MB of virtual memory is allocated. In practice, only a page or two will be allocated to hold the base of the stack, and of course any memory required for the request's data.
- You can decrease the default stack size, and would if you were trying to work with a lot of threads.
Why?
> Newer concurrency systems will handle thousands or millions of lightweight processes without firing up large numbers of native threads, and there's a reason for that design decision.
I would say that is based on the historically accurate but currently false notion that OS threads are expensive to start up or keep idle.
For example, see this benchmark of Python web servers: http://nichol.as/benchmark-of-python-web-servers
Why do you think that? What constraints do you believe limit the number of threads you can use?
- An idle thread is essentially free.
- The memory used by an ongoing request should not be inherently different between an evented server and a threaded server.
- The number of sockets an evented server can use is the same as the number of sockets available to a threaded server.
If evented servers are handling this better, all it shows in my opinion is that the threaded servers have been written incorrectly or else configured incorrectly. But that is not an inherent benefit of evented servers. Please correct me if there is a constraint I am missing.