处理Java Out of Memory问题步骤
1. 收集与分析verbose gc 的错误讯息输出
* 将“verbosegc“参数加入命令提示列启动Server,这将会将GC 的活动信息显示在标准输出/输入,转到stdout/stderr 的档案中。执行应用程序直到问题再次产生。
* 确定在java out of memory 之前,JVM 做如下内容:
* Full GC run:
执行full GC 与所有soft/weak/phantomly reachable 的对象能被移除与这些空间能被回收,在下面网址可以找到更多不同等级对象消耗细项说明:
http://java.sun.com/developer/technicalArticles/ALT/RefObj
你能检查full GC 在out of memory 讯息之前做,接下来的一个讯息显示当GC 已经完成(讯息的格式是依照JVM 定义,检查JVM 的Help message 以了解讯息格式):
[memory ] 7.160: GC 131072K->130052K (131072K) in 1057.359 ms
下面是上面格式的说明(注意:相似格式将会被使用在遍及这份文件):
[memory ] <start>: GC <before>K-><after>K (<heap>K), <total> ms
[memory ] <start> - start time of collection (seconds since jvm start)
[memory ] <before> - memory used by objects before collection (KB)
[memory ] <after> - memory used by objects after collection (KB)
[memory ] <heap> - size of heap after collection (KB)
[memory ] <total> - total time of collection (milliseconds)
然而,这无法使用讯息推断soft/weak/phantomly reachable objects 被移除。假如GC 算法是generational 算法,你会看到verbose 输出像这样:
[memory ] 2.414: Nursery GC 31000K->20760K (75776K), 0.469 ms
上述是nursery GC(或young GC)循环将会提升正在执行的objects 从
nursery(或young space)到old space。这个循环不是重要的分析,更详细项目有关generational 算法可以在JVM 的文件中找到;假如GC 的循环不是发生在java out of memory,这样就可能是JVM 的bug。
* Full compaction:
保证JVM 做适当的紧密程度工作,内存没有碎裂,能防止large objects被配置而且引发一个java out of memory 错误。Java Objects 需要连续的记忆区块,假如没有空的内存区块,那么JVM 将无法配置一个large objects,它将无法符合任何一个空的区块。在这个情况下JVM 将会做full compaction,这样才会有更多连续内存区块能够符合容纳large objects。Compaction 工作包含移动objects(data)从java heap memory 一个区堆到另一个与更新references 到这些objects 的指定的新的位置上。JVM 将不会重排所有objects,除非这是需要的。这是减少GC 循环的暂停时间。我们能否透过verbose gc 讯息检查出内存碎裂的java out of memory。
假如我们看到输出像下列所示,即使仍有空的java heap 而out of memory 还是会发生,是因为内存碎裂的原因。
[memory ] 8.162: GC 73043K->72989K (131072K) in 12.938 ms
[memory ] 8.172: GC 72989K->72905K (131072K) in 12.000 ms
[memory ] 8.182: GC 72905K->72580K (131072K) in 13.509 ms
java.lang.OutOfMemoryError
以上的情况你能够看出max heap 是被设定128MB 与JVM 丢出out ofmemory 当物理内存使用只有72580K,Heap 使用率只有55%。因此,即使当有45%free heap 的时候,在这个案例内存碎裂的影响仍会丢出out of
memory。这是JVM 的bug 或是限制。你必需联络原厂请求协助。
2. 假如JVM 运作是正常的
假如JVM 运作是正常的(的在上述所提及的动作),那么java out of memory可能是应用程序的问题。这个应用程序可能不断泄漏一些java memory,可能是会引发这个问题。或者是应用程序使用过多的objects 它需要更多的heapmemory,以下是可以在这个应用程序中做检查的:
* 应用程序的caching:
假如应用程序在内存中caches java objects,那么我们将需要确定这个cache 是否一直在成长;可能需要在cache 中限制objects 的数量。我们能试着降低这个限制看看是否它会降低java heap 使用量。JAVA soft references 像softly reachable objects 一样使用data caching,当JVM 执行发生out of heap 时,是允许被移除。
* 执行过久的objects:
假如在应用系统里有在Heap 中存在过久的objects,那么我们可以尽可能地试着降低存在的objects。例如:调校HTTP session timeout 将能帮助更快回收无效session objects。
* Memory leaks:
一个memory leak 的范例是在应用系统中使用database connectionpools。当使用connection pools,JDBC statement 与resultset objects必需确定最后有被关闭。这基于事实,这由于从pool 中的connection
objects 使用呼叫close(),将简单connection 传回到pool 以提供重新使用,若没有真正关闭connection 与关联到的statement/resultset
objects。
* 增加java heap:
我们也能试着增加java heap 假如可能的话看是否能解决问题。
3. 假如不是属于前两者的状况:
那们我们需要使用JVMPI(JVM Profiler Interface)基础profiler 像Jprobe 或OptimizeIt 去找出那些Objects 是占住java heap,profilers 也从这些对象正被建立的地方,在java code 里的细节地方上。这份数据不包括在每profiler 上的细节。profiler 文件可能提到有关如何设定与启动应用系统与profilers。 通常,基于JVMPI
的profilers 会有过多负担与大大降低应用系统的效能。因此,在上线环境里使用这些profilers 是不适当的。
For Native OOM Problem:
1. 收集下列信息
* –verbosegc output to monitor the java heap usage.
这将能帮助了解应用系统的java memory 需求。应用系统它可能会需要独立使用的实际java heap,在JVM 启动时预留最大的heap 设定(使用-Xmx 参数在java 命令提示列)与在其他用途是不允许预留内存。在这个Jrockit 的案例,使用-verbose 取代-verbosegc 如同在GC 信息中增加给codegen 信息。* 记录Process virtual memory size 从应用系统被启动时,开始在JVM执行到out of native memory;这将能帮助了解不管是process 真的达到操作系统的大小限制。在Windows 的案例,遵照下列程度监控virtual process size:在开始\执行,输入”perfmon”并且按OK在Performance 图形上方按下“+“按键选择下面项目:
性能对象: Process (不是预设的processor)
从清单选取计数器: Virtual Bytes
从清单选择例项: Select the JVM (java) instance
按 “新增”, 并且 “关闭”
在Unix 或Linux 中对于PID 而言,virtual memory size 能够使用这个命令看到:– ps –p <PID> -o vsz.
在Linux 中每一个java thread 都会有单一个JVM instance 以process方式显示,假如我们选择一个root java process 的PID,这个root javaprocess 可以使用ps 命令加上-forest 参数能够被发现,例如:ps –IU
<user> --forst 将会显示某一特定用户所有process 的ASCII 的树状结构显示,你还可以从这树状结构找到root java。
2. 机器上可用的内存
假如机器RAM 与swap space 是不足,那么操作系统将无法提供更多的内存给process,那么也可能造成out of memory 的结果,确定同机器上的RAM
与swap space 在硬盘空间的加总是足够执行所有的Process。
3. Java heap 调校
假如java heap 的使用都在max heap 之内,那么可以考虑降低max heap的大小,以提供更多的native memory 给JVM 使用。这并不是一个解决方案,但是一个测试的变通方法。因为操作系统限定process 大小,我们需要在javaheap 与native heap 之间达到一个平衡。
4. JVM 的Native memory 使用量
在所有的classes 被加载JVM 的Native memory 使用量是将可预期会趋于平稳,并且方法已被呼叫(code 的产生已经结束)。应用系统通常发生在最初的几个小时,在那之后,JVM 可能在执行时加载class,code 的优化只需要很小的native memory。
为了减少问题,试着取消执行时优化并且检查是否有何不同。* 在这个jrockit 案例,参数-Xnoopt 能够取消执行时优化。在SUN的JVM,参数-Xint 将强制JVM执行在interpreted mode(nocode generation)。假如执行时native memory 使用量仍持续成长,那么问题就可能是在native
code 这边发生。
5. 应用程序中Third party 的native modules 或JNI code
检查不论你是使用Third party 的native module 像是database driver,这些native modules 可能也会配置native memory 并且漏洞可能就是这些modules。基于缩小问题,你能试着重建问题在不使用协助厂商的driver,例如:你能使用pure java drivers 取代native database drivers。检查你的应用系统是否使用JNI 的程序代码,这也可能造成native memory泄漏,并且你也可以尝试执行应用系统尽量不使用JNI 程序代码。
6. 假如照上述步骤仍无法找出native memory 问题
那么你就得向JVM 厂商取得能侦测native memory 配置的特别的版本并且更多有关JVM 上有关内存泄漏的讯息。
结论
系统开发时往往问题发生out of memory,或内存一直成长无法透过GC回到平均值时,这往往不知道该如何处理此问题所在,到底是应用系统问题,还是JDK 发生的问题,以上的这些是针对有关Memory leak 问题进行说明的了解,与问题排解原则加以说明,期望日后再碰到此问题能有所参考依据与检测的方式,以让此类问题能够不再发生。
何謂Java heap, Native memory and Process Size
一般在3-tier 架构下的应用系统,最常見的问题就是Out of Memory(内存不足),或Memory leak(内存泄漏)的狀况,最后往往导致Application
Server失效与系统Crash,让管理人员必需常常守候在Server 旁边,注意它关心它系统狀况与运作情形避免它Crash。而本专题报导则是针对此類型问题
的发生,从JVM 其基本架构开始說明,并采用问答与实例的方式进行說明解释,并且提供检查的项目說明,可藉由这些项目自我检测,以避免发生Memory
leak 有效掌握Resource。
何谓Out of Memory?Memory leak?
Out of Memory 定义
内存不論Java heap 或Native Memory 是不足以提供给组件使用。
Memory leak 定义
组件的内存使用不論在Java heap 或Native Memory 中持续成长,
最后导致发生Out of memory 的情况。
何谓Java heap, Native memory and Process Size
要了解Memory leak 问题的发生,首先先了解几个JVM 的内存管理重要的名称:
Java heap
这是JVM 用來配置Java objects 的内存,Java heap 内存大小是透过命令执行列中下的參數-Xmx 设定的。假如最大的heap size 没有定义,那么它的
大小限制将由JVM 视当时情况如机器的物理内存与剩下可用的内存决定。因此一般都建议要设定最大的Java heap 的值。
Native Memory
这是JVM 用來它内部运作的内存,Native Memory Heap 将是会被JVM使用,而它的大小取决于产生的程序代码、产生的thread、GC
时用于保存javaobject 信息与产生或优化程序代码时的暂存空间假如它是Third party 的native module,它将可能使用Native Memory.例如:native
JDBC driver 就是配置Native Memory。
Native Memory 的最大值是会受限于任何一OS 的virtual process size 与已经被參數-Xmx 指定给Java Heap 的内存大小。例如:假如应用系统能够
总共配置3GB,并且假如最大的Java Heap 大小为1G,那么Native Memory 最大值可能接近2GB。
Process Size
Process Size 将会是Java Heap、Native Memory 与被用于已加载执行与函式库的内存的加总,在32 位的操作系统,一个处理程序虚拟寻址空间能
够到4GB;若超过4GB,操作系统的核心将会预留一部份给它自己使用(一般是1~2GB)。那么剩下的就是给应用系统。Windows:假设有4GB 的内存,预
设给应用系统使用最大2GB 与而另外的2GB
是给核心使用。仅管如此,在一些不同版本的Windows,可以用/3GB參數切换这个比例值,让应用系统可以得到3GB 的内存。详细可參考Microsoft网站,
址址:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ddtools/hh/ddtools/bootini_1fcj.asp
Red Hat AS 2.1:应用系统可以使用到最大内存大小为3GB其他的操作系统,请參考该操作系统文件做设定。
Process 寻址空间与物理内存的不同
每一个Process 都拥有自己的寻址空间,在32 位操作系统,这个寻址空间是介于0 到4GB 之间。这是机器上独立的RAM 或Swap Space,机器上全部
的物理内存是同一台机器上的RAM 与Swap Space 的加总,所有执行中的Process 分享这个物理内存。Process 的内存寻址是虚拟的。操作系统
核心对应这虚拟地址到实体地址。实体地址指到物理内存中的某一个位置。在一台机器上任何特定时间所有被正在执行的Process 的虚拟内存,其
加总不能超出在同一台机器上全部物理内存大小。
为何会Out of Memory 问题发生?在这个狀况发生
JVM 会做什么处置?
Out of Memory in java heap
假如JVM 无法在java heap 取得内存來配置更多的java objects,JVM 会丢出java out of memory 错误,JVM 无法配置更多的java objects 假如heap
是塞满了正在使用的objects 与java heap 无法再扩展。在这个狀况下,在丢出java.lang.OutOfMemoryError 错误讯息后,JVM 会让应用系统决定要做什
么。例如:应用系统自行监控这个错误并且决定在那一个的模式下停止执行,或者不管这个错误。假如应用系统不处理这个错误,那么thread 会丢出这个错
误讯息并且停止执行離开JVM(假如使用java threaddump,你将不会看到这个thread)。
WebLogic Server 在这个狀况,假如它是由一个execute thread 丢出,这个错误将会被监控并且会做记錄。假如这是連续不断被丢出來,那么core
healthmonitor thread 会停止WebLogic Server 运作。
Out of Memory in native heap
假如无法native memory 配置到内存空间,则JVM 丢出native out ofmemory,这通常发生在Process 达到操作系统Process size 的限制或是机器
执行超出RAM 与Swap Space 加总.当这个发生时,JVM 会处理native out memory 情况,记錄讯息說明它执行到 out of native memory 或无法取得内存
并且離开。假如JVM 或任何被加载的module(像是libc 或是一个Third party 的module)无法处理这个native out ofmemory 狀况,然后操作系统将会传
送一个sigabort 讯息给JVM,这样将会使JVM 停止離开。通当JVM 将会产生程序代码文件当它取得sigabort 讯号。
Diagnosing java.lang.OutOfMemoryError ( 诊断Java内存溢出)
* Exception in thread “CompilerThread1″ java.lang.OutOfMemoryError: requested 793020 bytes for Chunk::new. Out of swap space?
Out of memory while reading in symbol table of /apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl
( 0) 0xc8461230 [/apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl]
( 1) 0xc80a5fec [/apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl]
( 2) 0xc7f00420 [/apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl]
( 3) 0xc7f00ca0 [/apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl]
( 4) 0xc8368d08 [/apps/bea/weblogic92/jdk1.5.0.18/opt/java1.5/jre/lib/PA_RISC2.0/server/libjvm.sl]
( 5) 0xc005b2e4 __pthread_body + 0×44 [/usr/lib/libpthread.1]
( 6) 0xc0065574 __pthread_start + 0×14 [/usr/lib/libpthread.1]
Java out of memory messages are marked with pid: 13828 in /var/adm/syslog/syslog.log.
Possible causes:
- not enough swap space left, or
- kernel parameter MAXDSIZ is very small.
Solution:
Although it appears that an OutOfMemoryError is thrown this apparent exception is reported by the HotSpot VM code when an allocation from the native heap failed and the native heap may be close to exhaustion. The message indicates the size (in bytes) of the request that failed and also indicates what the memory is required for. In some cases the reason will be shown but in most cases the reason will be the name of a source module reporting the allocation failure. If an OutOfMemoryError with this error is thrown it may require using utilities on the operating system to diagnose the issue further. Examples of issues that may not be related to the application are when the operating system is configured with insufficient swap space, or when there is another process on the system that is consuming all memory resources. If neither of these issues is the cause then it is possible that the application is failed due to native leak; for example, application or library code is continuously allocating memory but is not releasing it to the operating system.
For more information: http://java.sun.com/j2se/1.5/pdf/jdk50_ts_guide.pdf
The recommendation for swap space size in the Solaris is that swap should be configured about 30% of physical RAM.
The following link has suggested a workaround to add ‘-XX:+UseDefaultStackSize -Xss256K’ parameter.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4916142
Add: -XX:CodeCacheMinimumFreeSpace=2M -XX:+ReservedCodeCacheSize=64M -XX:PermSize=128m -XX:MaxPermSize=384m (As per your other JVM settings)
Sun team needs to be involved for this issue. Usually such issues are solved by sun support.
=============================
* java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:574)
at weblogic.work.RequestManager.createThreadAndExecute(RequestManager.java:271)
at weblogic.work.RequestManager.executeIt(RequestManager.java:245)
at weblogic.work.ServerWorkManagerImpl.schedule(ServerWorkManagerImpl.java:142)
Solution:
1) Set kernel parameter maxdsiz to a higher value
2) Reduce the current heap size.
3) Check the kernel values: ulimit -a
4) If the NPROC soft limit is lower than the hard limit, increase it as needed: ulimit -u <new value>.
Check the Operating System documentation to make changes permanent at the OS configuration files.
5) Need to reduce the JVM stack size and the OS stack size both. Set the -xss in java options and set ulimit -s on the OS level.
=============================
* java.lang.OutOfMemoryError: PermGen space
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
Solution: Increase the max permgen space -XX:MaxPermSize=256m
There can be a leak in the permgen objects. If tuning parameters do not resolve the issue, we need to use the memory leak detector tools and find out which instances in the permgen space are not getting cleared.
=============================
* java.lang.OutOfMemoryError: allocLargeObjectOrArray – Object size: 372032, Num elements: 372012
* java.lang.outofmemoryerror: nativeGetNewTLA
Solution: -XXtlasize:128k -XXlargeobjectlimit:128k
If this does not solve the issue, we need to check in the application code for the large objects being created and not being destroyed. Take JRA Recording (Oracle JRockit) or use JConsole and memory leak detector tools (JMAP, JHAT) for analysis on the
=============================
* java.lang.OutOfMemoryError: Java Heap Space
Solution: First thing that needs to be checked is the gc logs. Need to check whether the garbage collection is happening properly. If the heap keeps gradually increasing even after full gc, tune the gc algorithms and check if the behavior is the same. Use memory leak detector tools for both sun jdk and JRockit to check which instances from the application are not getting destroyed.
If this is not the case and the application genuinely needs more memory, increase the heap size by using the parameters:
Example: -Xms2048m -Xmx2048m
=============================
* java.lang.StackOverflowError
Solution: Stack over flow error is usually generated due to a recursive call made by the application (infinite recursion), or its because of an attempt to allocate more memory on the stack than will fit. This is usually the result of creating local array variables that are far too large for the current stack.
For the first possibility, we need to check the application code as to where is the recursive call being made.
For the second possibility, we can increase the JVM stack size by the parameter : Example: -Xss512K
=============================
