What is the maximum number of threads that can be created by a process under Linux?
How (if possible) can this value be modified?
Join Stack Overflow to learn, share knowledge, and build your career.
|
What is the maximum number of threads that can be created by a process under Linux? How (if possible) can this value be modified? |
|||
|
|
|
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:
The default is the number of memory pages/4. You can increase this like:
There is also a limit on the number of processes (and hence threads) that a single user may create, see |
|||||||||||||||||||||
|
|
This is WRONG to say that LINUX doesn't have a separate threads per process limit.
Thus, the number of threads per process can be increased by increasing total virtual memory or by decreasing stack size. But, decreasing stack size too much can lead to code failure due to stack overflow while max virtual memory is equals to the swap memory. Check you machine: Total Virtual Memory: Total Stack Size: Command to increase these values:
*Replace new value with the value you want to put as limit. References: http://dustycodes.wordpress.com/2012/02/09/increasing-number-of-threads-per-process/ |
|||||
|
|
In practical terms, the limit is usually determined by stack space. If each thread gets a 1MB stack (I can't remember if that is the default on Linux), then you a 32-bit system will run out of address space after 3000 threads (assuming that the last gb is reserved to the kernel). However, you'll most likely experience terrible performance if you use more than a few dozen threads. Sooner or later, you get too much context-switching overhead, too much overhead in the scheduler, and so on. (Creating a large number of threads does little more than eat a lot of memory. But a lot of threads with actual work to do is going to slow you down as they're fighting for the available CPU time) What are you doing where this limit is even relevant? |
|||||||||||||||||
|
|
To retrieve it:
To set it:
123456789 = # of threads |
||||
|
|
|
@dragosrsupercool Linux doesn't use the virtual memory to calculate the maximum of thread, but the physical ram installed on the system
http://kavassalis.com/2011/03/linux-and-the-maximum-number-of-processes-threads/ kernel/fork.c
So thread max is different between every system, because the ram installed can be from different sizes, I know Linux doesn't need to increase the virtual memory, because on 32 bit we got 3 GB for user space and 1 GB for the kernel, on 64 bit we got 128 TB of virtual memory, that happen on Solaris, if you want increase the virtual memory you need to add swap space. |
||||
|
|
|
proper 100k threads on linux:
|
|||||||||||||
|
|
Thread count limit: $ cat /proc/sys/kernel/threads-max How it is calculated: max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE); and: x86_64 page size (PAGE_SIZE) is 4K; Like all other architectures, x86_64 has a kernel stack for every active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big; for mempages : cat /proc/zoneinfo | grep spanned | awk ‘{totalpages=totalpages+$2} END {print totalpages}’; so actually the number is not related with limitation of thread memory stack size (ulimit -s). ps: thread memory stack limitation is 10M in my rhel VM, and for 1.5G memory, this VM can only afford 150 threads? |
|||
|
|
|
It probably shouldn't matter. You are going to get much better performance designing your algorithm to use a fixed number of threads (eg, 4 or 8 if you have 4 or 8 processors). You can do this with work queues, asynchronous IO, or something like libevent. |
|||||
|
|
Use |
||||
|
|
|
Depends on your system, just write a sample program [ by creating processes in a loop ] and check using ps axo pid,ppid,rss,vsz,nlwp,cmd. When it can no more create threads check nlwp count [ nlwp is the number threads ] voila you got your fool proof answer instead of going thru books |
|||
|
|
|
For anyone looking at this now, on systemd systems (in my case, specifically Ubuntu 16.04) there is another limit enforced by the cgroup pids.max parameter. This is set to 12,288 by default, and can be overriden in /etc/systemd/logind.conf Other advice still applies including pids_max, threads-max, max_maps_count, ulimits, etc. |
|||
|
|
|
check the stack size per thread with ulimit, in my case Redhat Linux 2.6:
Each of your threads will get this amount of memory (10MB) assigned for it's stack. With a 32bit program and a maximum address space of 4GB, that is a maximum of only 4096MB / 10MB = 409 threads !!! Minus program code, minus heap-space will probably lead to an observed max. of 300 threads. You should be able to raise this by compiling and running on 64bit or setting ulimit -s 8192 or even ulimit -s 4096. But if this is advisable is another discussion... |
|||
|
|
|
We can see the maximum number of threads defined in the following file in linux cat /proc/sys/kernel/threads-max (OR) sysctl -a | grep threads-max |
|||
|
|
|
To set permanently,
and add
|
|||
|
|