First, to understand the question:
Let's start with the following test procedure: The operating environment (it is necessary to illustrate that different environments will have different results): 32-bit Windows Xp,sun JDK 1.6.0_18, Eclipse 3.4, test program:
Java code
- Import Java.util.concurrent.CountDownLatch;
- Public class Testnativeoutofmemoryerror {
- public static void Main (string[] args) {
- For (int i = 0;; i++) {
- System.out.println ("i =" + i);
- New Thread (new Holdthread ()). Start ();
- }
- }
- }
- Class Holdthread extends Thread {
- Countdownlatch CDL = New Countdownlatch (1);
- Public Holdthread () {
- This.setdaemon (true);
- }
- public Void Run () {
- try {
- Cdl.await ();
- } catch (Interruptedexception e) {
- }
- }
- }
Import Java.util.concurrent.countdownlatch;public class Testnativeoutofmemoryerror {public static void main (string[] args) {for (int i = 0;; i++) {System.out.println ("i =" + i); new Thread (New Holdthread ()). Start ();}} Class Holdthread extends Thread {countdownlatch CDL = new Countdownlatch (1);p ublic holdthread () {This.setdaemon (true);} public void Run () {try {cdl.await ();} catch (Interruptedexception e) {}}}
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 Tes Tnativeoutofmemoryerror.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 JV Mmemory JVM Memory reservedosmemory reserved operating system memory Threadstacksize line stacks size
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 the program memory that is started by default is 64M Reservedosmemory is generally 130M around threadstacksize 32-bit JDK 1 .6 The default stacksize around 325K formula is as follows: (2*1024*1024-64*1024-130*1024)/325 = 5841 Formula calculated 5841, Basically consistent with practice 5602 (biased because reservedosmemory cannot be very precise)
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 by 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 = 452Exactly 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 uses 64-bit OS B, jvmmemory reduces the allocation of jvmmemory to C, threadstacksize reduces the stack size of a single thread
Resolution-java.lang.OutOfMemoryError:unable to create new native thread (reprint)