How many threads can a Java VM support?
Asked Answered
D

14

261

How many threads can a Java VM support? Does this vary by vendor? by operating system? other factors?

Dyanne answered 18/4, 2009 at 15:6 Comment(0)
G
216

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.

Grume answered 18/4, 2009 at 15:16 Comment(0)
R
104

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)
Rani answered 18/4, 2009 at 15:9 Comment(24)
How much memory did you start the JVM with? That matters.Grume
the default. Questioner has a test program now, and I want another cup of coffee.Rani
Okay, 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.Rani
I'm embarrassed to say that my laptop can only start a little over 900 Threads with your test program before it chokes.Grume
WinXP (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.Vinculum
But I can have multiple such JVMs with 5000 threads (see Windows Task Manager | Performance | Totals | Threads), so apparently the OS limit is much higher.Vinculum
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.Rani
I'd expect that, a Windows thread is a per-process resource. Linux threads are lightweight processes managed by the general scheduler.Rani
... aha. For the good reason that Java 6 won't run on this elderly mac mini I use as a writing machine.Rani
Java 6 update 13, Ubuntu 8.10 32 Bit, 4Gig ram, Default JVM settings = 6318 Threads.Veratridine
Heh, play with the thread stack size. java -Xss100k allowed me to create 19702 threads in Linux.Veratridine
java -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.Veratridine
See, 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.Rani
I'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.Embolism
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.Rani
Using 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.Infer
There'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.Eparch
A program that takes care of that can be found here. OK, I wrote it, but it's not advertising, it's all in the name of objective science :)Eparch
I'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..Hardin
Using 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.Exudation
Java 8 + Linux Mint 17.3 + 64bit i5-M430 + 8GB RAM DDR3 + Default JVM Settings = 30,593 ThreadsCrossbeam
On 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.Menarche
Processor - 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 threadElecampane
Karan, what OS? Since it's an OutOfMemory error, the first place to look is the heap size. This may help docs.oracle.com/cd/E13222_01/wls/docs81/perform/JVMTuning.htmlRani
S
61

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.

Spinster answered 18/4, 2009 at 16:18 Comment(4)
would make sense if EACH thread is given a heap of that size.Dinnie
Caveat: 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...Spinster
The heap size reduces the address space available for stacks. Address space of 256K/stack makes sense.Bunghole
Aye, this shows the same thing pequenoperro.blogspot.com/2009/02/less-is-more.htmlLemon
B
42

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.

Beta answered 21/4, 2013 at 8:13 Comment(4)
"Slows down" sound like swapping.Dinnie
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 :)Beta
Fried core 2 duo with ubuntu seasoning :DMopes
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.Iceblink
S
38

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.

Sontag answered 18/4, 2009 at 20:8 Comment(2)
So use a 64-bit OS. How long have we all been using 64-bit processors for now?Bunghole
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...Sontag
V
3

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.

Veratridine answered 18/4, 2009 at 17:54 Comment(2)
Can we name it DieLikeACat? it won't be dead nor alive until you run it.Housekeeper
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.Dannie
G
1

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.

Garlaand answered 18/4, 2009 at 15:32 Comment(4)
could you look again? I would like to see it - it sounds interesting and support that functional languages are easy to scale across cores.Dinnie
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: 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?Endora
It was the "Ants" simulation I believe - here's a link where Rich Hickey (Clojure creator) talks about this - 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.Ortegal
@Ortegal lins has expired.Dinnie
S
1

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

Sarcoid answered 1/4, 2017 at 13:17 Comment(0)
P
1

Year 2017... DieLikeADog class.

New thread #92459 Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread

i7-7700 16gb ram

Presentative answered 11/10, 2017 at 20:46 Comment(1)
Offcourse answers will vary. I got 10278 with 6 GB of ram.Eisele
S
1

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();
        }
    }
 }
Stereophonic answered 3/4, 2021 at 16:7 Comment(1)
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.\Godly
C
1

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

Congruency answered 21/6, 2023 at 17:41 Comment(0)
D
0

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

Dashtikavir answered 15/12, 2010 at 8:59 Comment(0)
W
0

Maximum number of threads depends on following things:

  • Hardware Configuration like microprocessor, RAM.
  • Operating System like whether it is 32-bit or 64-bit
  • Code inside the run method. If code inside the run method is huge then single thread object will have more memory requirement
  • Wakerobin answered 29/5, 2013 at 4:5 Comment(0)
    C
    -5

    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();
            }
        }
    }
    
    Contain answered 10/12, 2010 at 9:13 Comment(4)
    I 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.Monochromatism
    @Monochromatism 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.Gardie
    However, 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.Gardie
    amazing ... a discovery of the actual Turing machine ... infinite resources.Gammer

    © 2022 - 2024 — McMap. All rights reserved.