Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify the issue when Java OutOfMemoryError?

How to identify the issue when java OutOfMemoryError or stackoverflow comes in production. By which reason it is coming or why the server is down.

For example I am developing an application which is lived on production and UAT. Instantly on production java OutOfMemoryError or stackoverflow.

Then how can we track this issue, by which reason it has happened ? Is there any technique that can tell me by which code flow this is happening ?

Please explain it. I have faced this issue many times.

like image 685
Rahul Tripathi Avatar asked Aug 27 '14 05:08

Rahul Tripathi


4 Answers

If you face it in production and you cannot really reason about it from stacktraces or logs, you need to analyze what was in there.

Get the VM to dump on OOM

-XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath="/tmp"

And use that for analysis. The memory analyzer tool (http://eclipse.org/mat/) is a good standalone program for this analysis.

like image 127
Niels Bech Nielsen Avatar answered Oct 12 '22 04:10

Niels Bech Nielsen


The Oracle docs:- Troubleshooting Memory Leaks has detailed explanation on it:

This error is thrown when there is insufficient space to allocate an object in the Java heap or in a particular area of the heap. The garbage collector cannot make any further space available to accommodate a new object, and the heap cannot be expanded further.

.....

An early step to diagnose an OutOfMemoryError is to determine what the error means. Does it mean that the Java heap is full, or does it mean that the native heap is full? To help you answer this question, the following subsections explain some of the possible error messages, with reference to the detail part of the message:

Exception in thread "main": java.lang.OutOfMemoryError: Java heap space

See 3.1.1 Detail Message: Java heap space.

Exception in thread "main": java.lang.OutOfMemoryError: PermGen space

See 3.1.2 Detail Message: PermGen space.

Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit

See 3.1.3 Detail Message: Requested array size exceeds VM limit.

Exception in thread "main": java.lang.OutOfMemoryError: request bytes for . Out of swap space?

See 3.1.4 Detail Message: request bytes for . Out of swap space?.

Exception in thread "main": java.lang.OutOfMemoryError: (Native method)

See 3.1.5 Detail Message: (Native method).

UPDATE:-

You can download the HotSpot VM source code from OpenJDK. If you want to monitor and track the memory footprint of your Java Heap spaces ie, the young generation and old generation spaces is to enable verbose GC from your HotSpot VM. You may add the following parameters within your JVM start-up arguments:

-verbose:gc –XX:+PrintGCDetails –XX:+PrintGCTimeStamps –Xloggc:<app path>/gc.log
like image 24
Rahul Tripathi Avatar answered Oct 12 '22 06:10

Rahul Tripathi


You can use jvisualvm to manage your process at the runtime.

You can see memory, heap space, objects etc ...

This program is located in your bin directory of your JDK.

like image 2
zatenzu Avatar answered Oct 12 '22 05:10

zatenzu


It's best to try to reproduce the problem in place where you are free to debug it - on dev server or on your local machine. Then try to debug, look for recursive invocations, heap size and what objects are being created. Unfortunately it's not always easy to reproduce prod env (with it's load and so on) on local machine, therefore finding the root cause of such error might be a challenge.

like image 1
Marcin Szawurski Avatar answered Oct 12 '22 06:10

Marcin Szawurski