5

I am getting an OutOfMemoryError: Java heap space

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

asked Apr 4, 2014 at 15:37
2
  • 1
    Yes, they are called profilers. As a recommendation, use Eclipse Memory Analyzer a.k.a. MAT . Commented Apr 4, 2014 at 15:38
  • Or the VisualVM which comes with the JDK, or you could use one of the many commercial profilers such as YourKit. Commented Apr 4, 2014 at 15:39

3 Answers 3

6

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
answered Apr 4, 2014 at 16:23

1 Comment

-XX:+HeapDumpOnOutOfMemoryError also dumps Metaspace? If not, how to analyze it?
3

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.

answered Apr 4, 2014 at 15:40

Comments

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
answered Apr 4, 2014 at 16:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.