内存溢出,无法创建新的本地线程的原因和解决方案,java.lang.OutOfMemoryError: unable to create new native thread
I recently came across this exception on a couple of java systems that
use many threads java.lang.OutOfMemoryError: unable to create new
native thread. The strange thing was that the JVM had been assigned a
lot of memory (1.5GB) and that it had at least half the memory
available. Michele found this article that points out that the more
memory you give to the JVM the more likely you are to get
java.lang.OutOfMemoryError: unable to create new native thread
exceptions when you have many threads.
JVM已经分配了足够多的内存了(1.5GB),而且至少有一半的可用内存。但是当你有很多的线程在运行时,你给JVM的内存越多,你越容易出现无法创建本地线程的内存溢出。
Which makes perfect sense when you think about it. Each 32 bit
process on Windows has 2GB "available" memory as 2GB is reserved to
Windows. In my case the JVM grabbed 1.5 GB leaving 500MB. Part of the
500MB was used to map system dlls etc in memory so less than 400 MB was
left. Now to the crucial point: When you create a thread in java it
creates a Thread object in the JVM memory but it also creates a
operating system thread. The operating system creates the thread with a
thread stack in the 400MB that is left, not in the 1.5 GB allocated in
the JVM. Java 1.4 uses a default stack size of 256kb but Java 1.5 uses
a 1MB stack per thread. So, in the 400MB left to process I could only
generate ~400 threads. Absurd but true: to create more threads you have
to reduce the memory allocated to the JVM. Another option is to host
the JVM in your own process using JNI.
在32位的Windows机器上,最大有2G的内存,所以JVM一般使用1.5G留下500M。其中的一部分用来映射系统的dll到内存里面,所以只剩下了400M.此时,我们创建了一个java线程,它不仅在JVM的内存创建了线程对象,同时创建了操作系统的线程。
操作系统创建线程,使用了400M里的线程堆栈而不是JVM里面1.5GB的。 每个线程,Java 1.4默认使用 256k, 1.5默认使用 1M的堆栈。那么 400MB的内存,最多可以创建400个线程。为了创建更多的线程,你必须减少你的JVM分配的内存数。
This formula gives a decent estimate for the number of threads you can create:
(MaxProcessMemory - JVMMemory - ReservedOsMemory) / (ThreadStackSize) = Number of threads
For Java 1.5 I get the following results assuming that the OS reserves about 120MB:
1.5GB allocated to JVM: (2GB-1.5Gb-120MB)/(1MB) = ~380 threads
1.0GB allocated to JVM: (2GB-1.0Gb-120MB)/(1MB) = ~880 threads
Java 1.4 uses 256kb for the thread stack which lets you create a lot more threads:
1.5GB allocated to JVM: ~1520 threads
1.0GB allocated to JVM: ~3520 threads
I have not tried the 3GB switch but it should in theory let you create more threads.
分享到:
相关推荐
【Java虚拟机内存溢出分析】:当遇到`java.lang.OutOfMemoryError: unable to create new native thread`错误时,这通常表示系统无法为新的Java线程分配足够的内存,即操作系统层面的资源耗尽,而非Java堆内存不足。...
5. 无法创建新的原生线程(Unable to create new native thread) 当JVM尝试创建新的线程时,如果无法获取足够的原生内存来分配新线程的栈空间,就会抛出该错误。通常发生在系统限制了线程数量或是原生内存不足时。 ...
- **错误日志**:`java.lang.OutOfMemoryError: unable to create new native thread` 和 `java.lang.OutOfMemoryError: request bytes for ... Out of swap space?` - **原因**:可能由于Java堆设置过大导致...
【情况五】:`java.lang.OutOfMemoryError: unable to create new native thread` 这表明系统无法创建新的原生线程,可能是由于线程栈空间不足或者系统资源限制。解决方法包括减少单个线程栈大小(使用`-Xss`参数)...
2. java.lang.OutOfMemoryError: unable to create new native thread 该错误是由于Stack空间不足以创建额外的线程,要么是创建的线程过多,要么是Stack空间确实小了。解决方法是通过-Xss启动参数减少单个线程栈...
- **java.lang.OutOfMemoryError: unable to create new native thread**:无法为线程分配内存。 - **java.lang.OutOfMemoryError: request bytes for**:地址空间不足。 解决内存溢出问题,首先需要查看监控工具...
3. **OutOfMemoryError: unable to create new native thread** - 当系统无法为新的线程分配内存时,会出现此类错误。这通常是由于操作系统级别的限制导致的。 #### 二、内存溢出的原因分析与解决策略 ##### 1. ...
第五种是无法创建新的本地线程(Unable to create new native thread)。当应用程序尝试创建过多线程,超出了操作系统的限制时,就会出现这个错误。解决方法是减少线程的创建数量,优化线程的使用,或者提升操作系统...
- **无法创建新原生线程**(`java.lang.OutOfMemoryError: unable to create new native thread`):当操作系统没有足够的资源来创建新线程时发生,可以通过调整`-Xss`参数来减少分配给单个线程的栈空间大小。...