谁经常会遇到这样的问题。总的想法是改变配置启动参数,试图增加参数,我想,这能避免内存溢出。但效果基本上是错误的。我发现了一篇文章在互联网上解决这个问题 主要观点为
这个异常问题本质原因是我们创建了太多的线程,而能创建的线程数是有限制的。导致了异常的发生。能创建的线程数的详细计算公式例如以下:(MaxProcessMemory - JVMMemory - ReservedOsMemory) / (ThreadStackSize) = Number of threadsMaxProcessMemory 指的是一个进程的最大内存JVMMemory JVM内存ReservedOsMemory 保留的操作系统内存ThreadStackSize 线程栈的大小在java语言里, 当你创建一个线程的时候。虚拟机会在JVM内存创建一个Thread对象同一时候创建一个操作系统线程,而这个系统线程的内存用的不是JVMMemory,而是系统中剩下的内存(MaxProcessMemory - JVMMemory - ReservedOsMemory)。
由公式得出结论:你给JVM内存越多,那么你能创建的线程越少,越easy发生java.lang.OutOfMemoryError: unable to create new native thread。
解决这个问题:1, 假设程序中有bug,导致创建大量不须要的线程或者线程没有及时回收,那么必须解决这个bug,改动參数是不能解决这个问题的。2, 假设程序确实须要大量的线程,现有的设置不能达到要求,那么能够通过改动MaxProcessMemory,JVMMemory,ThreadStackSize这三个因素,来添加能创建的线程数:a, MaxProcessMemory 使用64位操作系统b, JVMMemory 降低JVMMemory的分配c, ThreadStackSize 减小单个线程的栈大小这个观点让我開始也非常不解,但细致查看了一下出错日志
# There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocation (malloc) failed to allocate 2334888 bytes for Chunk::new# An error report file with more information is saved as:# D:\xxx_err_pid1904.log在查看具体的pid日志,则惊喜的发现其给出的解决方法与上面讲到的理论同样
## There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocation (malloc) failed to allocate 2355528 bytes for Chunk::new# Possible reasons:# The system is out of physical RAM or swap space# In 32 bit mode, the process size limit was hit# Possible solutions:# Reduce memory load on the system# Increase physical memory or swap space# Check if swap backing store is full# Use 64 bit Java on a 64 bit OS# Decrease Java heap size (-Xmx/-Xms)# Decrease number of Java threads# Decrease Java thread stack sizes (-Xss)# Set larger code cache with -XX:ReservedCodeCacheSize=# This output file may be truncated or incomplete.## Out of Memory Error (allocation.cpp:328), pid=4308, tid=6720## JRE version: 7.0_25-b16# Java VM: Java HotSpot(TM) Server VM (23.25-b01 mixed mode windows-x86 )# Failed to write core dump. Call to MiniDumpWriteDump() failed#注意上面标红色处 于是将tomcat服务启动參数都改小了一半,变为例如以下參数
-Xms256M-Xmx512M-Xss1M再启动服务,并马上观察任务管理中tomcat进程具体,内存使用情况较出错时内存使用上涨的最大值小了,并逐渐回落到一个比較低的值。而CPU的使用情况在刚启动时比出错时值要高一点,启动后回归正常。而且服务能够正常启动了。
综合上面的情况。并在简单的实践后。我觉得引用文章中的理论应该是有道理的。所下面对次再遇到这个异常。最好能尽量减少相关的配置参数。也许我们会解决这个问题。
版权声明:本文博主原创文章,博客,未经同意不得转载。