linux - [Solved-5 Solutions] Maximum number of threads per process in Linux - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
What is the maximum number of threads that can be created by a process under Linux?
Linux - Solution 1:
cat /proc/sys/kernel/threads-max
click below button to copy the code. By - Linux tutorial - team
The default is the number of memory pages/4. You can increase this like:
echo 100000 > /proc/sys/kernel/threads-max
click below button to copy the code. By - Linux tutorial - team
There is also a limit on the number of processes (and hence threads) that a single user may create.
Linux - Solution 2:
Linux doesn't have a separate threads per process limit.
Linux implements max number of threads per process indirectly
number of threads = total virtual memory / (stack size*1024*1024)
click below button to copy the code. By - Linux tutorial - team
Thus, the number of threads per process can be increased by increasing total virtual memory or by decreasing stack size.
Check you machine:
Total Virtual Memory: ulimit -v (default is unlimited, thus you need to increase swap memory to increase this)
Total Stack Size: ulimit -s (default is 8Mb)
Command to increase these values:
ulimit -s newvalue
ulimit -v newvalue
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 3:
Checking the current number of threads used
The following command will output the number of processes for the myfasuser (example) user. Note: Threads are counted as processes in linux
top -b -H -u myfasuser -n 1 | wc -l
click below button to copy the code. By - Linux tutorial - team
If the number is less than 200 try without the -H flag as the Thread toggle may have already been set in top
top -b -u myfasuser -n 1 | wc -l
click below button to copy the code. By - Linux tutorial - team
The highest of these 2 outputs indicates the number of myfasuser processes A base line Cafex installation will typically use around 700 threads in a quiescent state and this figure will rise over time with traffic and the full product set deployed.
Linux - Solution 4:
To retrieve it:
cat /proc/sys/kernel/threads-max
click below button to copy the code. By - Linux tutorial - team
To set it:
echo 123456789 > /proc/sys/kernel/threads-max
click below button to copy the code. By - Linux tutorial - team
Linux - Solution 5:
Linux doesn't use the virtual memory to calculate the maximum of thread, but the physical ram installed on the system
max_threads = totalram_pages / (8 * 8192 / 4096);
click below button to copy the code. By - Linux tutorial - team
kernel/fork.c
/* The default maximum number of threads is set to a safe
* value: the thread structures can take up at most half
* of memory.
*/
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
click below button to copy the code. By - Linux tutorial - team
- 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.