How many threads can a Java VM support?

前端 未结 12 1131
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:32

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

相关标签:
12条回答
  • 2020-11-22 07:10

    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)
    
    0 讨论(0)
  • 2020-11-22 07:10

    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.

    0 讨论(0)
  • 2020-11-22 07:14

    Year 2017... DieLikeADog class.

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

    i7-7700 16gb ram

    0 讨论(0)
  • 2020-11-22 07:18

    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.

    0 讨论(0)
  • 2020-11-22 07:21

    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.

    0 讨论(0)
  • 2020-11-22 07:21

    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

    0 讨论(0)
提交回复
热议问题