How many threads can be created by the JVM

Source: Internet
Author: User

maximum number of threads that the JVM can supportJVM Maximum number of threads(2012-07-04 23:20:15) reproduced
Tags: jvm Maximum number of threads it Category: Java Distributed summary

Excerpt from: http://sesame.iteye.com/blog/622670

Work encountered this problem several times, I feel it is necessary to summarize, so with this article, this article is divided into three parts: to understand the problem, analyze the problem, solve the problem.

First, to understand the question:

Let's start by following this test procedure to understand the problem:
Running environment (it is necessary to explain that different environments will have different results): 32-bit Windows Xp,sun JDK 1.6.0_18, Eclipse 3.4,
Test procedure:

Java code
  1. Import Java.util.concurrent.CountDownLatch;
  2. public class Testnativeoutofmemoryerror {
  3. public static void Main (string[] args) {
  4. for (int i = 0;; i++) {
  5. System.out.println ("i =" + i);
  6. New Thread (New Holdthread ()). Start ();
  7. }
  8. }
  9. }
  10. Class Holdthread extends Thread {
  11. Countdownlatch CDL = new Countdownlatch (1);
  12. Public Holdthread () {
  13. This.setdaemon (TRUE);
  14. }
  15. public void Run () {
  16. try {
  17. Cdl.await ();
  18. } catch (Interruptedexception e) {
  19. }
  20. }
  21. }

Without specifying any JVM parameters, run the output directly in eclipse and see this friend:
i = 5602
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:597)
At Testnativeoutofmemoryerror.main (testnativeoutofmemoryerror.java:20)

Second, the analysis of the problem:

The intrinsic reason for this anomaly is that we have created too many threads, and the number of threads that can be created is limited, causing the exception to occur. The number of threads that can be created is calculated as follows:
(maxprocessmemory-jvmmemory-reservedosmemory)/(Threadstacksize) = number of threads
Maxprocessmemory refers to the maximum memory of a process
Jvmmemory JVM Memory
Reservedosmemory reserved Operating system memory
Size of the Threadstacksize line stacks

In the Java language, when you create a thread, the virtual opportunity creates a thread object in the JVM memory to create an operating system threads, and the system thread's memory is not jvmmemory, but the remaining memory in the system (Maxprocessmemory- Jvmmemory-reservedosmemory).


with the above example, let's illustrate the formula:
Maxprocessmemory under 32-bit Windows is 2G
Jvmmemory Eclipse default startup program memory is 64M
Reservedosmemory is usually around 130M
Threadstacksize 32-bit JDK 1.6 default stacksize around 325K
The formula is as follows:
(2*1024*1024-64*1024-130*1024)/325 = 5841
Formula calculated 5841, and practice 5602 basically consistent (biased because the reservedosmemory can not be very accurate)

The formula concludes: The more memory you give to the JVM, the fewer threads you can create, the more likely the java.lang.OutOfMemoryError:unable to create new native thread will occur.

Gee, a bit of our common sense, well, let us verify, still use the above test program, plus the following JVM parameters, the test results are as follows:
Threadstacksize jvmmemory The number of threads that can be created
Default 325k-xms1024m-xmx1024m i = 2655
Default 325k-xms1224m-xmx1224m i = 2072
Default 325k-xms1324m-xmx1324m i = 1753
Default 325k-xms1424m-xmx1424m i = 1435
-xss1024k-xms1424m-xmx1424m i = 452
Exactly the same as the formula.

third, solve the problem:
1, if there is a bug in the program, resulting in the creation of a large number of unwanted threads or the thread is not timely recovery, you must resolve this bug, modify parameters is not solve the problem.
2, if the program does require a large number of threads, the existing settings do not meet the requirements, then you can modify the maxprocessmemory,jvmmemory,threadstacksize these three factors, to increase the number of threads that can be created:
A, maxprocessmemory using 64-bit operating system
B, jvmmemory reduce the distribution of jvmmemory
C, threadstacksize reduce the stack size of a single thread

Iv. Other:

In Java applications, there are times when such errors occur: Outofmemoryerror:unable to create new native thread. This strange thing is because the JVM has been allocated a lot of memory (such as 1.5G) by the system, And it consumes at least half of the available memory. It has been found that the more memory you allocate to the JVM, the greater the likelihood that the above error will occur, given the large number of threads.

Each 32-bit process can use up to 2G of available memory, because another 2G is reserved by the operating system. This assumes that 1.5G is used for the JVM, and then the remaining 500M of available memory. This 500M part of the memory must be used to load the system DLL, then the real left is perhaps only 400M, and now the key point arises: When you use Java to create a thread, in the JVM's memory will also create a thread object, But it also creates a real physical thread in the operating system (referencing the JVM specification), and the operating system creates the physical thread in the remaining 400 megabytes of memory, rather than in the 1500M memory heap of the JVM. In jdk1.4, the default stack size is 256KB, but in jdk1.5, the default stack size is 1M per thread, so we can create up to 400 available threads in the remaining 400M of available memory.

The conclusion is that to create more threads, you must reduce the maximum memory allocated to the JVM. Another option is to have the JVM host inside your JNI code.

Give an estimate formula for the maximum number of threads that can be created:

(maxprocessmemory-jvmmemory-reservedosmemory)/(Threadstacksize) = number of threads

For jdk1.5, assume that the operating system retains 120M of memory:
1.5GB JVM: (2GB-1.5GB-120MB)/(1MB) = ~380 threads
1.0GB JVM: (2GB-1.0GB-120MB)/(1MB) = ~880 threads

For jdk1.4 with a stack size of 256KB,
1.5GB allocated to JVM: ~1520 threads
1.0GB allocated to JVM: ~3520 threads

How many threads can be created by the JVM

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.