Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the maximum number of threads per process in Linux?
Linux manages thread allocation through several system parameters and limits. The maximum number of threads per process is determined by multiple factors including system-wide thread limits, virtual memory availability, and stack size configurations.
System-Wide Thread Limit
The first approach to check the maximum number of threads is to examine the system-wide limit:
cat /proc/sys/kernel/threads-max
61741
This value represents the total number of threads that can exist on the entire system. You can modify this limit using:
echo 123456789 > /proc/sys/kernel/threads-max
where 123456789 is your desired maximum thread count.
Per-Process Thread Calculation
Linux doesn't enforce a direct per-process thread limit. Instead, the maximum threads per process is calculated indirectly based on available resources:
number of threads = total virtual memory / (stack size * 1024 * 1024)
This formula shows that thread count depends on:
Virtual memory limit − Total memory available to the process
Stack size − Memory allocated per thread stack
Adjusting Thread Limits
Modifying Stack Size
To increase threads by reducing stack size per thread:
ulimit -s newvalue
Replace newvalue with the desired stack size in KB. Smaller stack size allows more threads.
Increasing Virtual Memory
To allow more threads by increasing virtual memory limit:
ulimit -v newvalue
Replace newvalue with the desired virtual memory limit in KB.
Key Factors Affecting Thread Count
| Factor | Impact on Thread Count | Default Value |
|---|---|---|
| Stack Size | Inversely proportional | 8192 KB (8 MB) |
| Virtual Memory | Directly proportional | System dependent |
| System threads-max | Global upper bound | ~61,000 (varies) |
Conclusion
The maximum threads per process in Linux is not a fixed limit but calculated based on virtual memory and stack size. You can increase thread capacity by reducing stack size or increasing virtual memory limits, while staying within the system-wide thread maximum.
