Tools for OutOfMemoryError java heap space analysis

☆樱花仙子☆ 提交于 2019-12-07 15:14:19

问题


I am getting an OutOfMemoryError: Java heap space

Are there any tools I can use to find the root cause ?


回答1:


You can analyse the heap dump of your application using some analysis tool like eclipse mat to see what's consuming how much of the heap.

But first you need to obtain the heap dump of your application.

To have the JVM automatically generate the heap dump for you when the OOM error occurs you can use the -XX:+HeapDumpOnOutOfMemoryError option. On top of that, you can also use the -XX:HeapDumpPath option to tell JVM where to generate the file.

java -XX:HeapDumpPath="D:\heapdumps\YourApp.hprof" -XX:+HeapDumpOnOutOfMemoryError -jar YourApp.jar

Once that file is generated you can open it in mat and do your ananlysis.


You can also manually generate the heap dump at any point while your application is running. For this purpose you can use the jmap command that comes with jdk.

jmap -dump:live,format=b,file="D:\heapdumps\YourApp.hprof" process_id_of_your_app

You can use the tool - jps, which also comes with jdk, to easily find the process id of your application.

jps -m



回答2:


Yes, they are called profilers. There are plenty options in the market. Just load the memory dump from the JVM in the profiler and it will show the memory usage and can help you to spot where the problems may lie.

The JDK comes with VisualVM and since Java SE 7 u 40 you have Java Mission Control (free license).

As a personal recommendation, use Eclipse Memory Analyzer a.k.a. MAT (free license) or Yourkit (commercial license) for memory memory dump analysis.

DISCLAIMER: I am not attached to any of these companies. Just providing info from a happy user of these tools.




回答3:


Add these JVM arguments, which would log the Garbage collection details to the log file.

-Xloggc:gc_memory_logs.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps 

The logs would looks like this,

1.703: [GC [PSYoungGen: 132096K->16897K(153600K)] 132096K->16905K(503296K), 0.0171210 secs] [Times: user=0.05 sys=0.01, real=0.01 secs] 
3.162: [GC [PSYoungGen: 148993K->21488K(153600K)] 149001K->22069K(503296K), 0.0203860 secs] [Times: user=0.04 sys=0.00, real=0.02 secs] 
4.545: [GC [PSYoungGen: 153584K->21484K(153600K)] 154165K->25309K(503296K), 0.0224490 secs] [Times: user=0.06 sys=0.01, real=0.02 secs] 
6.159: [GC [PSYoungGen: 153580K->21472K(285696K)] 157405K->33127K(635392K), 0.0271700 secs] [Times: user=0.08 sys=0.01, real=0.03 secs] 

Once you have this log, you could analyse the logs using many different tools (http://www.fasterj.com/tools/gcloganalysers.shtml). one such tools is garbagecat. https://code.google.com/a/eclipselabs.org/p/garbagecat/

Using this tool you could analyse the logs, which give results like this

========================================
SUMMARY:
========================================
# GC Events: 18
GC Event Types: PARALLEL_SCAVENGE
Max Heap Space: 967680K
Max Heap Occupancy: 700911K
Max Perm Space: 0K
Max Perm Occupancy: 0K
Throughput: 100%
Max Pause: 82 ms
Total Pause: 582 ms
First Timestamp: 1703 ms
Last Timestamp: 56428185 ms
========================================
ANALYSIS:
========================================
========================================
0 UNIDENTIFIED LOG LINE(S):
========================================

Once you know the results you could adjust the heap and permanent memory settings accordingly.

For example,

-Xms512m -Xmx2g -XX:PermSize=512m  -XX:MaxPermSize=2g

Apart from this, we could use other useful tools that comes along with JDK like,

jvisualvm
jconsole


来源:https://stackoverflow.com/questions/22867140/tools-for-outofmemoryerror-java-heap-space-analysis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!