Maximum Number of Threads Allowed to Run

Maximum number of threads per process in Linux?

Linux doesn't have a separate threads per process limit, just a limit on the total number of processes on the system (threads are essentially just processes with a shared address space on Linux) which you can view like this:

cat /proc/sys/kernel/threads-max

The default is the number of memory pages/4. You can increase this like:

echo 100000 > /proc/sys/kernel/threads-max

There is also a limit on the number of processes (and hence threads) that a single user may create, see ulimit/getrlimit for details regarding these limits.

How to run the maximum number of threads possible on your computer

The maximum number of threads you can run on Windows depend on how much memory you have. Each thread in Windows consumes by default 1MB of RAM for stack (plus some more for its context and a tiny kernel-mode stack). For a 32-bit app, the maximum number of threads you can create is about ~1800, provided you don't allocate any other memory. In other words, how many threads you can create boils down to how much free memory you application has.

But, you should not create too many threads. Normally, you should aim to have as many threads as cores available. Creating threads is expensive, switching between threads - too.

What is the maximum number of threads a process can have in windows

There is no limit that I know of, but there are two practical limits:

  1. The virtual space for the stacks. For example in 32-bits the virtual space of the process is 4GB, but only about 2G are available for general use. By default each thread will reserve 1MB of stack space, so the top value are 2000 threads. Naturally you can change the size of the stack and make it lower so more threads will fit in (parameter dwStackSize in CreateThread or option /STACK in the linker command). If you use a 64-bits system this limit practically dissapears.
  2. The scheduler overhead. Once you read the thousands of threads, just scheduling them will eat nearly 100% of your CPU time, so they are mostly useless anyway. This is not a hard limit, just your program will be slower and slower the more threads you create.

Is there a limit on the maximum number of threadpools created during an application lifetime?

(In Java 11) there isn't a specific limit on the number of thread pools, and there isn't a thread pool counter that might overflow.

There is an ID / sequence number for threads, but since its type is long and it is only incremented by one when a new Thread is created, overflow is not a practical concern1.

However, threads ... and particularly live threads2 ... use a significant amount of memory. So you can't have an unbounded number of active threads or thread pools in existence at the same time.


1 - Do the math. How long would it take an application to create 2^63 threads ... assuming there was enough memory, etc.

2 - Threads that have been started, and have not yet terminated.

What's the maximum number of threads possible for a threads in Windows 8.1?

Like most limits in Windows, this is limited by available memory. A 32-bit process keels over somewhat shy of 2000 threads when all available virtual memory is occupied by the stacks of the threads (1 MB each). A 64-bit process is limited by the size of the paging file, needed to commit the allocation. Many thousands, it depends on how fast the paging file can grow to meet the needs of the program. There is also a limit imposed by the kernel's paged memory pool, each thread has a kernel stack so that it can make kernel calls, typically 24 KB per thread.

These limits are far beyond the number of balls a programmer can keep in the air without dropping one on his foot. He'll be limping around for a long time, threading bugs are exceedingly hard to troubleshoot.

Mark Russinovich explores the limits in this excellent blog post.



Related Topics



Leave a reply



Submit