How Many Threads Can a Java JVM Actually Handle? Limits, Experiments, and Tips
This article explores the maximum number of threads a Java Virtual Machine can support, examining factors such as CPU, operating system, JVM version, and memory, and presents real‑world experiments, code examples, and theoretical calculations to help developers understand and manage thread limits effectively.
On StackOverflow, McGovernTheory asked how many threads a Java Virtual Machine can support and what factors affect this limit.
Eddie's answer
The number depends on CPU, operating system, other processes, Java version, and available memory. He observed a Windows server becoming unstable around 6500 Java threads.
Charlie Martin's answer
Thread limits are influenced by JVM runtime parameters and OS support. He provided a sample program that creates threads in an infinite loop.
class DieLikeADog {
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv) {
for (;;) {
new Thread(new Runnable() {
public void run() {
synchronized (s) {
count += 1;
System.err.println("New thread #" + count);
}
}
}).start();
try { Thread.sleep(1000); } catch (Exception e) { System.err.println(e); }
}
}
}Running this on Intel OS X 10.5.6 with Java 5 produced output until it hit an OutOfMemoryError after about 2550 threads.
Benjismith's experiment
He varied the JVM heap size from 2 MB to 1024 MB on Vista Home Premium SP1 with JDK 1.6.0_11 and recorded the maximum thread count. Larger heaps reduced the number of threads, showing an inverse relationship.
2 MB → 5744 threads
4 MB → 5743 threads
8 MB → 5735 threads
12 MB → 5724 threads
16 MB → 5712 threads
24 MB → 5687 threads
32 MB → 5662 threads
48 MB → 5610 threads
64 MB → 5561 threads
96 MB → 5457 threads
128 MB → 5357 threads
192 MB → 5190 threads
256 MB → 5014 threads
384 MB → 4606 threads
512 MB → 4202 threads
768 MB → 3388 threads
1024 MB → 2583 threadsNeil Coffey's answer
The theoretical maximum equals the process address space divided by the thread stack size. On a 32‑bit Windows system with a 2 GB address space and 128 KB stacks, the limit is about 16 384 threads, though in practice around 13 000 were observed.
He advises managing many threads efficiently and notes that thread stack size can be set in the Thread constructor, but it should not be confused with JVM heap settings.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
