Programing

Java VM이 몇 개의 스레드를 지원할 수 있습니까?

lottogame 2020. 5. 9. 09:04
반응형

Java VM이 몇 개의 스레드를 지원할 수 있습니까?


Java VM이 몇 개의 스레드를 지원할 수 있습니까? 이것은 공급 업체에 따라 다릅니 까? 운영 체제에 의해? 다른 요인?


이는 사용중인 CPU, OS, 다른 프로세스가 수행중인 작업, 사용중인 Java 릴리스 및 기타 요인에 따라 다릅니다. 컴퓨터를 종료하기 전에 Windows 서버에> 6500 스레드가있는 것을 보았습니다. 물론 대부분의 스레드는 아무것도하지 않았습니다. 기계가 약 6500 스레드 (Java에서)에 도달하면 전체 기계에 문제가 발생하여 불안정 해졌습니다.

내 경험에 따르면 Java (최신 버전)는 컴퓨터 자체가 문제없이 호스팅 할 수있는만큼 많은 스레드를 행복하게 사용할 수 있습니다.

물론 충분한 RAM이 있어야하며 스레드가 수행하는 모든 작업을 수행하고 각 스레드에 대한 스택을 갖기 위해 충분한 메모리로 Java를 시작해야합니다. 최신 CPU (가장 최근 몇 세대의 AMD 또는 Intel) 및 1-2 기가 메모리 (OS에 따라 다름)가있는 모든 시스템은 수천 개의 스레드가 있는 JVM을 쉽게 지원할 수 있습니다 .

이보다 더 구체적인 답변이 필요한 경우 가장 좋은 방법은 프로파일 링하는 것입니다.


음.

여기에는 몇 가지 매개 변수가 있습니다. 특정 VM뿐만 아니라 일반적으로 VM에 런타임 매개 변수도 있습니다. 그것은 운영 체제에 의해 다소 좌우됩니다. 기본 OS가 스레드에 대해 어떤 지원을 제공하고 어떤 제한이 있습니까? VM이 실제로 OS 수준 스레드를 사용하는 경우 오래된 빨간색 스레드 / 녹색 스레드가 좋습니다.

"지원"의 의미는 또 다른 질문입니다. 다음과 같은 Java 프로그램을 작성하는 경우

   class DieLikeADog {
         public static void main(String[] argv){
             for(;;){
                new Thread(new SomeRunaable).start();
             }
         }
    }

(그리고 작은 구문 세부 사항에 대해 불평하지 않고, 나는 첫 번째 커피 한잔에 있습니다) 그렇다면 수백 또는 수천 개의 스레드가 실행될 것으로 예상해야합니다. 그러나 스레드 작성 은 상대적으로 비싸며 스케줄러 오버 헤드가 심할 수 있습니다. 그 스레드가 유용한 것을 할 수 있는지 확실하지 않습니다.

최신 정보

알았어 저항 할 수 없었어 여기 몇 가지 장식이 포함 된 작은 테스트 프로그램이 있습니다.

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();
        }
    }
}

Intel의 OS / X 10.5.6 및 Java 6 5 (주석 참조)에서 얻을 수있는 내용은 다음과 같습니다.

새 스레드 # 2547
새 스레드 # 2548
새 스레드 # 2549
스레드를 만들 수 없습니다 : 5
새 실 # 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)

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.


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.


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.


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.


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.


Year 2017... DieLikeADog class.

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

i7-7700 16gb ram


If you are asking this question, you may want to rewrite your code.


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


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

  • 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


    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();
            }
        }
    }
    

    참고URL : https://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support

    반응형