Jvm supports Simple testing of the maximum number of threads and jvm supports the maximum number of threads.

Source: Internet
Author: User
Tags xms

Jvm supports Simple testing of the maximum number of threads and jvm supports the maximum number of threads.

Recently, to test the maximum number of concurrent threads in Openfire, a large number of threads are required to simulate the client. I am always confused about the number of threads that a JVM instance can open. Therefore, I plan to test the number of threads that may be affected by google:

-Xms

Intial java heap size

-Xmx

Maximum java heap size

-Xss

The stack size for each thread

System Restrictions

Maximum number of open threads in the system

The test procedure is as follows: Java code:

import java.util.concurrent.atomic.AtomicInteger;    public class TestThread extends Thread {    private static final AtomicInteger count = new AtomicInteger();      public static void main(String[] args) {      while (true)        (new TestThread()).start();      }      @Override    public void run() {      System.out.println(count.incrementAndGet());        while (true)        try {          Thread.sleep(Integer.MAX_VALUE);        } catch (InterruptedException e) {          break;        }    }  }  

Test environment: System: Ubuntu 10.04 Linux Kernel 2.6 (32-bit)
Memory: 2 GB
JDK: 1.7

Test results:
Ø ignore System Restrictions

-Xms

-Xmx

-Xss

Result

1024 m

1024 m

1024 k

1737

1024 m

1024 m

64 k

26077

512 m

512 m

64 k

31842

256 m

256 m

64 k

31842

When the number of created threads reaches 31842, no threads can be created in the system.

The above test results show that increasing the heap memory (-Xms,-Xmx) will reduce the number of threads that can be created and increase the thread stack memory (-Xss, in a 32-bit system, the minimum value of this parameter is 60 K. This reduces the number of threads that can be created.

Combine System Restrictions

The limit of 31842 threads is determined by the maximum number of threads that can be generated by the system:/proc/sys/kernel/threads-max. The default value is 32080. Modify the value to 10000: echo 10000>/proc/sys/kernel/threads-max. The modified test result is as follows:

-Xms

-Xmx

-Xss

Result

256 m

256 m

64 k

9761

In this case, does it mean you can configure as many threads as possible? Modify echo 1000000>/proc/sys/kernel/threads-max. The modified test result is as follows:

-Xms

-Xmx

-Xss

Result

256 m

256 m

64 k

32279

128 m

128 m

64 k

32279

It is found that the number of threads will not increase after reaching 32279. Check that the maximum number of PIDs that can be created in a 32-bit Linux system is 32678. The value can be modified through/proc/sys/kernel/pid_max (the modification method is the same as threads-max ), however, in 32 systems, this value can only be changed to a smaller value, but cannot be larger. When threads-max is specified, the test result of modifying pid_max is as follows:

Pid_max

-Xms

-Xmx

-Xss

Result

1000

128 m

128 m

64 k

582

10000

128 m

128 m

64 k

9507

The situation on Windows should be similar, but the number of threads created on Windows may be less than that on Linux. A server based on the thread model is always limited by the number of threads.

The maximum number of threads that can be generated in JVM is determined by the JVM heap memory size, Thread Stack memory size, and the maximum number of threads that can be created in the system. (The implementation of Java threads is based on the Thread mechanism of the underlying system. implemented, windows _ beginthreadex and Linux pthread_create. The specific quantity can be estimated based on the maximum memory (2 GB for 32-bit systems) that Java processes can access, heap memory, and Thread Stack memory.

Order:

In a 64-bit Linux system (CentOS6, 3G memory), another parameter is found to limit the number of threads: maxuserprocess (which can be viewed through ulimit-a. The default value is 1024, you can modify this value through ulimit-u). This value is not limited in the above 32-bit Ubuntu test environment.

Change the value of threads-max, pid_max, and maxuserprocess to 100000,-Xms and-Xmx as small as possible (128 m, 64 m ), -The Xss should be as small as possible (the minimum value is 104 k in 64-Bit mode and the value can be 128 k ). In such a test environment, the number of threads is limited by the memory size (3 GB) of the test environment. However, the actual test result is that the number of threads reaches 32 KB (32768, JVM throws a warning: Attempttoallocatestackguardpagesfailed. Then, an OutOfMemoryError occurs and the local thread cannot be created. Check the memory and find that there is still a lot of idle space, so it should not be the reason for the memory capacity. Google's warning is fruitless. I do not know why for the moment. Further research is required.

Order 2:

I accidentally found this article [7] Today and immediately tried it. This factor will affect the number of threads created, doubling the number of/proc/sys/vm/max_map_count as described in this article, from 65536 to 131072, the total number of threads created has reached 65000 +, and the computer is basically stuck (3g memory )... The function of this parameter is briefly checked. The description in [8] is as follows:

"Thisfilecontainsthemaximumnumberofmemorymapareasaprocessmayhave. Memorymapareasareusedasaside-appstofcallingmalloc, directlybymmapandmprotect, andalsowhenloadingsharedlibraries.

Whilemostapplicationsneedlessthanathousandmaps, certainprograms, participant lymallocdebuggers, mayconsumelotsofthem, e.g., uptooneortwomapsperallocation.

Thedefaultvalueis65536 ."

OK, this problem is finally solved. Finally, we will summarize the factors that affect the number of Java threads:

Java Virtual Machine itself:-Xms,-Xmx,-Xss;

System Restrictions:

/Proc/sys/kernel/pid_max,

/Proc/sys/kernel/thread-max,

Max_user_process (ulimit-u ),

/Proc/sys/vm/max_map_count.

Summary

The above is all about the simple testing of jvm support for the maximum number of threads. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.