How many threads can a Java VM support? Does this vary by vendor? by operating system? other factors?
14 Answers
This depends on the CPU you're using, on the OS, on what other processes are doing, on what Java release you're using, and other factors. I've seen a Windows server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.
My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.
Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.
If you need a more specific answer than this, your best bet is to profile.

- 53,828
- 22
- 125
- 145
Um, lots.
There are several parameters here. The specific VM, plus there are usually run-time parameters on the VM as well. That's somewhat driven by the operating system: what support does the underlying OS have for threads and what limitations does it put on them? If the VM actually uses OS-level threads at all, the good old red thread/green thread thing.
What "support" means is another question. If you write a Java program that is just something like
class DieLikeADog {
public static void main(String[] argv){
for(;;){
new Thread(new SomeRunaable).start();
}
}
}
(and don't complain about little syntax details, I'm on my first cup of coffee) then you should certainly expect to get hundreds or thousands of threads running. But creating a Thread is relatively expensive, and scheduler overhead can get intense; it's unclear that you could have those threads do anything useful.
Update
Okay, couldn't resist. Here's my little test program, with a couple embellishments:
public 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);
}
for(;;){
try {
Thread.sleep(1000);
} catch (Exception e){
System.err.println(e);
}
}
}
}).start();
}
}
}
On OS/X 10.5.6 on Intel, and Java 6 5 (see comments), here's what I got
New thread #2547 New thread #2548 New thread #2549 Can't create thread: 5 New thread #2550 Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:592) at DieLikeADog.main(DieLikeADog.java:6)

- 1
- 1

- 110,348
- 25
- 193
- 263
-
13How much memory did you start the JVM with? That matters. – Eddie Apr 18 '09 at 15:53
-
the default. Questioner has a test program now, and I want another cup of coffee. – Charlie Martin Apr 18 '09 at 16:07
-
2Okay, you tempted me, @Eddie, but messing about with -Xmx and -Xss just lead to a lot of combinations that don't work well. I need an undergrad who wants a research project. – Charlie Martin Apr 18 '09 at 16:16
-
I'm embarrassed to say that my laptop can only start a little over 900 Threads with your test program before it chokes. – Eddie Apr 18 '09 at 16:17
-
2WinXP (32bit), C2Q6600, 4GB RAM, jdk1.6.0_11 (32bit) with default JVM options gets me #5587 threads. Strangely with more JVM memory I can create less threads: with the "-Xms1G -Xmx1G" JVM options only #2644. – Esko Luontola Apr 18 '09 at 16:26
-
2But I can have multiple such JVMs with 5000 threads (see Windows Task Manager | Performance | Totals | Threads), so apparently the OS limit is much higher. – Esko Luontola Apr 18 '09 at 16:35
-
Yeah, the Windows JVM gets lots of attention, it's usually about the best. Oddly, even better than Solaris JVM on some things. The thing is the number of threads is going to be a function of all of -Xms -Xmx and -Xss, with the answer being an optimum among the three. – Charlie Martin Apr 18 '09 at 16:36
-
I'd expect that, a Windows thread is a per-process resource. Linux threads are lightweight processes managed by the general scheduler. – Charlie Martin Apr 18 '09 at 16:38
-
... aha. For the good reason that Java 6 won't run on this elderly mac mini I use as a writing machine. – Charlie Martin Apr 18 '09 at 16:44
-
11Java 6 update 13, Ubuntu 8.10 32 Bit, 4Gig ram, Default JVM settings = 6318 Threads. – Steve K Apr 18 '09 at 17:32
-
12Heh, play with the thread stack size. java -Xss100k allowed me to create 19702 threads in Linux. – Steve K Apr 18 '09 at 17:40
-
23java -Xss50k got me around 32k threads. That maxxed out my 4gigs of ram though. I had to stop some running processes to get enough memory back on my machine to fork a new process to kill java ;) - good times. – Steve K Apr 18 '09 at 17:46
-
1See, that's the thing. -Xss is allocating space for the separate thread stacks; that's competing for space with the heap, and limited by -Xmx. So you really need to explore at least three dimensions, and probably more. As I say, this calls for an undergrad research project. – Charlie Martin Apr 18 '09 at 18:28
-
1I'm getting almost the same results as you, Charlie Martin, on my Intel OSX 10.5.6 with both Java 5 and Java six with various combination of Xss, Xmx and Xms. There is something else in play here. – Stu Thompson Apr 19 '09 at 09:00
-
The OS/X implementation is pretty different from the others, different code base, which is why it took so long to get Java 6. We've certainly established the base point of "it depends". I'll ping some of my erstwhile colleagues at Sun and see what I can find out. – Charlie Martin Apr 19 '09 at 13:29
-
22Using Java 7 on windows 7 I just created 200,000 threads before my system died. Tasks Manager showed the process using 8GB of RAM. Not sure why it stopped there, though ... I have 12GB of RAM on my computer. So this may be hitting some other limit. – Dobes Vandermeer Mar 26 '12 at 10:35
-
6There's a flaw in this program: many more threads could have been started and unable to get to the line where they print their thread id. Then the OOM exception will happen and you'll have a number indeterminately lower than the real number of threads. – Marko Topolnik May 20 '12 at 18:58
-
4A program that takes care of that can be found [here](http://stackoverflow.com/a/10676165/1103872). OK, I wrote it, but it's not advertising, it's all in the name of objective science :) – Marko Topolnik May 20 '12 at 19:13
-
1I'm crashed at 38000 within eclipse and 71000 within cmd in Windows 8.1 x64 6 GB Ram Core i5 3rd Gen.. Anyhow, I had to hard restart my computer.. – The Coder Jun 01 '15 at 19:40
-
1Using Java 8 on Windows 7 64-bit, I managed to get 400,000 threads running on (just below) 20GB of RAM, at which the memory usage loitered at that mark and did not go above it. – mgthomas99 Jun 13 '16 at 20:43
-
1Java 8 + Linux Mint 17.3 + 64bit i5-M430 + 8GB RAM DDR3 + Default JVM Settings = 30,593 Threads – Aaron Esau Dec 30 '16 at 23:11
-
1On OSX this program generates 4,975 threads before failing no matter what I set the heap size to. Two gigs, twelve gigs, it's all the same. – Jon Strayer Apr 18 '19 at 20:59
-
1Processor - 2.2 GHz 6-Core Intel Core i7 Memory - 16 GB 2400 MHz DDR4 Was able to create around 4050 threads. How can I increase this number? ----------Exception in thread "main" New thread #4069 java.lang.OutOfMemoryError: unable to create new native thread – Karan Arora Feb 27 '21 at 21:44
-
Karan, what OS? Since it's an OutOfMemory error, the first place to look is the heap size. This may help https://docs.oracle.com/cd/E13222_01/wls/docs81/perform/JVMTuning.html – Charlie Martin Feb 27 '21 at 21:52
After reading Charlie Martin's post, I was curious about whether the heap size makes any difference in the number of threads you can create, and I was totally dumbfounded by the result.
Using JDK 1.6.0_11 on Vista Home Premium SP1, I executed Charlie's test application with different heap sizes, between 2 MB and 1024 MB.
For example, to create a 2 MB heap, I'd invoke the JVM with the arguments -Xms2m -Xmx2m.
Here are my results:
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 threads
So, yeah, the heap size definitely matters. But the relationship between heap size and maximum thread count is INVERSELY proportional.
Which is weird.

- 16,559
- 9
- 57
- 80
-
12would make sense if EACH thread is given a heap of that size. – Thorbjørn Ravn Andersen Apr 18 '09 at 18:56
-
1Caveat: my machine does not have 2583 GB of RAM. Or swap. And the JVM doesn't allocate thread-local heap space. So that can't be it... – benjismith Apr 18 '09 at 20:37
-
56The heap size reduces the address space available for stacks. Address space of 256K/stack makes sense. – Tom Hawtin - tackline Apr 20 '09 at 12:00
-
1Aye, this shows the same thing http://pequenoperro.blogspot.com/2009/02/less-is-more.html – Toby Dec 10 '10 at 16:43
I know this question is pretty old but just want to share my findings.
My laptop is able to handle program which spawns 25,000
threads and all those threads write some data in MySql database at regular interval of 2 seconds.
I ran this program with 10,000 threads
for 30 minutes continuously
then also my system was stable and I was able to do other normal operations like browsing, opening, closing other programs, etc.
With 25,000 threads
system slows down
but it remains responsive.
With 50,000 threads
system stopped responding
instantly and I had to restart my system manually.
My system details are as follows :
Processor : Intel core 2 duo 2.13 GHz
RAM : 4GB
OS : Windows 7 Home Premium
JDK Version : 1.6
Before running I set jvm argument -Xmx2048m
.
Hope it helps.

- 11,438
- 36
- 130
- 186
-
2
-
Thanks. It was pretty solid machine and worked fine for almost 8 years until suddenly it stopped booting up. I just installed Ubuntu on it instead of Windows and it started crunching numbers again :) – Shekhar Dec 19 '18 at 16:08
-
-
I came across a similar problem in a production environment, one of my colleagues forget to shut down a thread pool so every time executing that code will cause the number of thread increases. And as a result, the response was very slow, at first we know the cpu usage was very high and we knew the "vm thread" was the top 1 cpu-consuming thread. After jstack we checked the jvm memory monitor, but there was nothing special. At last in the calalina.log we found something abnormal, there were too many errors about "thread cannot be recycled". I spend lots of time on that and I want a method to. – Jack Sep 25 '20 at 00:21
The absolute theoretical maximum is generally a process's user address space divided by the thread stack size (though in reality, if all your memory is reserved for thread stacks, you won't have a working program...).
So under 32-bit Windows, for example, where each process has a user address space of 2GB, giving each thread a 128K stack size, you'd expect an absolute maximum of 16384 threads (=2*1024*1024 / 128). In practice, I find I can start up about 13,000 under XP.
Then, I think you're essentially into whether (a) you can manage juggling that many threads in your code and not do obviously silly things (such as making them all wait on the same object then calling notifyAll()...), and (b) whether the operating system can. In principle, the answer to (b) is "yes" if the answer to (a) is also "yes".
Incidentally, you can specify the stack size in the constructor of the Thread; you don't need to (and probably shouldn't) mess about with VM parameters for this.

- 21,615
- 7
- 62
- 83
-
1So use a 64-bit OS. How long have we all been using 64-bit processors for now? – Tom Hawtin - tackline Apr 20 '09 at 12:01
-
Sure, I'm just giving an example of a theoretical vs practical limit. Mind you, there are an awful lot of 32-bit machines (including servers) still out there... – Neil Coffey Apr 20 '09 at 12:19
After playing around with Charlie's DieLikeACode class, it looks like the Java thread stack size is a huge part of how many threads you can create.
-Xss set java thread stack size
For example
java -Xss100k DieLikeADog
But, Java has the Executor interface. I would use that, you will be able to submit thousands of Runnable tasks, and have the Executor process those tasks with a fixed number of threads.

- 19,408
- 6
- 52
- 50
-
16Can we name it DieLikeACat? it won't be dead nor alive until you run it. – Goodwine Aug 14 '13 at 21:38
-
Thank you for pointing at Executors, people should use it more often. But it won't work if the `Runnable`/`Callable` actually needs to run continuously, like when it has to handle communication. But it's perfect for SQL queries. – Matthieu Oct 24 '14 at 08:28
Additional information for modern (systemd) linux systems.
There are many resources about this of values that may need tweaking (such as How to increase maximum number of JVM threads (Linux 64bit)); however a new limit is imposed by way of the systemd "TasksMax" limit which sets pids.max on the cgroup.
For login sessions the UserTasksMax default is 33% of the kernel limit pids_max (usually 12,288) and can be override in /etc/systemd/logind.conf.
For services the DefaultTasksMax default is 15% of the kernel limit pids_max (usually 4,915). You can override it for the service by setting TasksMax in "systemctl edit" or update DefaultTasksMax in /etc/systemd/system.conf

- 1
- 1

- 1,832
- 1
- 15
- 13
Year 2017... DieLikeADog class.
New thread #92459 Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
i7-7700 16gb ram

- 598
- 5
- 7
when i was research this topic in my 2GB Samsung AMD processor laptop running Trisquel linux (Ubuntu 18.04) . It can manage 9534 threads and then after that it throw peculiar Exception
at java.base/java.lang.Thread.start0(Native Method) at java.base/java.lang.Thread.start(Thread.java:803) at Main.main(Main.java:11)
code :
public class MultithreadingRunnable implements Runnable {
public void run() {
System.out.println("ThreadID " + Thread.currentThread().getId());
}
}
public class Main {
public static void main(String[] ars) {
for(int i = 0;i<10000;i++){
Thread mr = new Thread(new MultithreadingRunnable());
mr.start();
}
}
}

- 17,953
- 10
- 93
- 108

- 1,116
- 1
- 10
- 19
-
Could you please let me know how to run this on my server? I have some weird errors and want to check the threads as you mention.\ – sunta3iouxos Jan 20 '22 at 18:41
I recall hearing a Clojure talk where he got to run one of his apps on some specialized machine at a trade show with thousands of cores (9000?), and it loaded them all. Unfortunately, I can't find the link right now (help?).
Based on that, I think it's safe to say that the hardware and your code are the limiting factors, not the JVM.

- 572
- 2
- 5
-
could you look again? I would like to see it - it sounds interesting and support that functional languages are easy to scale across cores. – Thorbjørn Ravn Andersen Apr 18 '09 at 18:57
-
Can you provide a link to that? I know that Cliff Click, Jr., the Lead Engineer from Azul Systems ran Rich Hickey's Ant Colony Simulation on Azul's largest JCA system (Azul Vega 3 Series 7300 Model 7380D: http://AzulSystems.Com/products/compute_appliance_specs.htm ) with 864 cores and 768 GB RAM, and the 700 ants managed to max out 700 cores. But 9000 cores, that's pretty impressive. What kind of machine was that? – Jörg W Mittag Apr 18 '09 at 22:10
-
It was the "Ants" simulation I believe - here's a link where Rich Hickey (Clojure creator) talks about this - http://blip.tv/clojure/clojure-concurrency-819147 . It was on some big Azul systems box with 800+ cores, mainly done to demonstrate how good Clojure is at handling multi-core concurrency. – mikera Jul 18 '11 at 19:50
-
Maximum number of threads depends on following things:

- 89
- 3
At least on Mac OS X 10.6 32bit, there is a limit (2560) by the operating system. Check this stackoverflow thread.

- 1
- 1

- 5,037
- 4
- 30
- 43
With Virtual Threads (previewed in JDK 19, finalized in JDK 21), multiple people have reported spinning up over 10 million threads.
Here is the JEP for more information: https://openjdk.org/jeps/444

- 86,244
- 97
- 390
- 689
You can process any number of threads; there is no limit. I ran the following code while watching a movie and using NetBeans, and it worked properly/without halting the machine. I think you can keep even more threads than this program does.
class A extends Thread {
public void run() {
System.out.println("**************started***************");
for(double i = 0.0; i < 500000000000000000.0; i++) {
System.gc();
System.out.println(Thread.currentThread().getName());
}
System.out.println("************************finished********************************");
}
}
public class Manager {
public static void main(String[] args) {
for(double j = 0.0; j < 50000000000.0; j++) {
A a = new A();
a.start();
}
}
}
-
7I know this is comment is very late but I believe the reason you were able to start and run these many threads is because (assuming normal and reasonable system configurations) each thread basically prints out one line out of output and then has no more reason to exist and is killed soon after, thereby ensuring a continuous availability of resources. – ucsunil Oct 13 '14 at 08:10
-
@ucsunil That's not true. I think you've misread the code. I just tried it and the different threads are printing out not just the first line, but their names too. Which means that they are active. If you thought that garbage collection will recycle unreferenced threads, it doesn't, [see here](http://stackoverflow.com/q/2423284/1143274). – Evgeni Sergeev Feb 26 '16 at 15:04
-
1However, after just a short while, `main` will throw an `OutOfMemoryError` on my machine, saying that it can't create any more threads. Perhaps @AnilPal, you didn't notice that. I suggest including another print statement in the `main(..)` method, to see clearly when it ceases to create new threads after throwing the error. – Evgeni Sergeev Feb 26 '16 at 15:06
-
5amazing ... a discovery of the **actual** Turing machine ... infinite resources. – Josh Sep 29 '16 at 14:43