Resolution-java.lang.OutOfMemoryError:unable to create new native thread

Source: Internet
Author: User

First, to understand the question:

First we pass the following Test the program to understand the problem:
Operating Environment (it's worth explaining 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 = " Span style= "Color:black" > + 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's 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

Resolution-java.lang.OutOfMemoryError:unable to create new native thread

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.