I. Understanding:
First, we use the following
Test procedure
To understand this problem:
Running Environment (it is worth noting that different environments have different results): 32-bit Windows XP, Sun JDK 1.6.0 _ 18, eclipse 3.4,
Test procedure:
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) { } } }
If no JVM parameter is specified, eclipse directly runs the output:
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)
Ii. analyze the problem:
The root cause of this exception is that we have created too many threads, and the number of threads that can be created is limited, resulting in exceptions. The formula for calculating the number of threads that can be created is as follows:
(Maxprocessmemory-jvmmemory-reservedosmemory)/(threadstacksize) = number of threads
Maxprocessmemory refers to the maximum memory of a process.
Jvmmemory JVM memory
Operating system memory retained by reservedosmemory
Threadstacksize the size of the thread Stack
In Java, when you create a thread, the virtual opportunity creates a thread object in the JVM memory and creates an operating system thread at the same time. The memory of this system thread is not jvmmemory, but the remaining memory in the system (maxprocessmemory-jvmmemory-reservedosmemory ).
The formula is described based on the above example:
Maxprocessmemory is 2 GB in 32-bit windows
By default, jvmmemory eclipse starts 64 MB of program memory.
Reservedosmemory is usually around MB
Threadstacksize 32-bit JDK 1.6 default stacksize about K
The formula is as follows:
(2*1024*1024-64*1024-130*1024)/325 = 5841
The formula calculates 5841, which is basically the same as the practice 5602 (there is a deviation because reservedosmemory cannot be accurate)
According to the formula, the more JVM memory you provide, the less threads you can create, and the more likely java. Lang. outofmemoryerror: Unable to create new Native thread.
Callback, a bit back to our common sense. Well, let's verify that the above test program is still used, and the following JVM parameters are added. The test result is as follows:
Threadstacksize the number of threads that can be created by jvmmemory
Default 325 K-xms1024m-xmx1024m I = 2655
Default 325 K-xms1224m-xmx1224m I = 2072
Default 325 K-xms1324m-xmx1324m I = 1753
Default 325 K-xms1424m-xmx1424m I = 1435
-Xss1024k-xms1424m-xmx1424m I = 452
Completely consistent with the formula.
Iii. Problem Solving:
1. If there is a bug in the program and a large number of unnecessary threads are created or the threads are not recycled in time, the bug must be solved. modifying parameters cannot solve the problem.
2. If the program requires a large number of threads and the existing settings cannot meet the requirements, you can modify maxprocessmemory, jvmmemory, and threadstacksize to increase the number of threads that can be created:
A. maxprocessmemory uses a 64-bit Operating System
B. jvmmemory reduces jvmmemory allocation.
C. threadstacksize: reduce the stack size of a single thread
3,
Reduce-xss512k: Set the stack size of each thread. After JDK 256, the size of each thread stack is 1 MB, and the size of each previous thread stack is K. The memory size required for more application threads is adjusted. Within the same physical
Reduce this value to generate more threads. However, the operating system still has a limit on the number of threads in a process. It cannot be generated infinitely. The experience is between 3000 and ~ About 5000.
4. Reduce-xms512m-xmx512m
5. Upgrade the CPU to 64-bit