20

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.

Kevin Panko
  • 7,844
  • 19
  • 46
  • 58
Rahul Tripathi
  • 515
  • 2
  • 7
  • 15
  • First look at the stacktrace if you call a method recursivly. – Jens Aug 27 '14 at 05:33
  • Follow the stack traces. If nothing found attach a profiler in your test environment and run some load testing. – Juned Ahsan Aug 27 '14 at 05:35
  • Stack traces can be misleading, since it is likely that not the offender (the process, that consumed the most memory) will throw an exception – Danubian Sailor Aug 27 '14 at 09:41
  • possible duplicate of [What is an OutOfMemoryError and how do I debug and fix it](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug-and-fix-it) – Raedwald Aug 31 '14 at 07:16

6 Answers6

19

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.

Eli Acherkan
  • 6,251
  • 2
  • 26
  • 34
Niels Bech Nielsen
  • 4,459
  • 1
  • 18
  • 42
  • have you heard any tracker for which i can analyse which release has send to production.any thread tracker. – Rahul Tripathi Aug 27 '14 at 06:49
  • not sure I understand. Do your application not assemble a version number inside the jar files? And do they not print it on console when started? – Niels Bech Nielsen Aug 27 '14 at 07:03
  • 2
    Note that the `HeapDumpOnOutOfMemoryError` flag must be prefixed by `-XX:+` and not `-XX:-` (some documentation gets it wrong). I'm editing your answer to fix this. – Eli Acherkan Aug 27 '14 at 10:06
  • @ntoskrnl E.g. this page makes it look like it works with a minus instead of a plus: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html – Eli Acherkan Aug 28 '14 at 05:58
  • That page isn't wrong, @Eli. The table column heading includes **default value** to indicate to the reader that the heap dump option is disabled by default. The emphasis could be made a lot clearer though. – Dave Jarvis Sep 14 '20 at 18:33
6

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
Community
  • 1
  • 1
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
  • You are defining what error means, which is not the question. – Juned Ahsan Aug 27 '14 at 05:36
  • @JunedAhsan:- `has detailed explanation on it` – Rahul Tripathi Aug 27 '14 at 05:36
  • if issue come on production that is on other country and I am in india. I need solve issue instantly.On production side any server log will generated to track the issue or any logger which is the source that can give me tracking issue.(because code has been done my side and after sending to production i need only solution ) – Rahul Tripathi Aug 27 '14 at 06:14
2

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.

user3666197
  • 1
  • 6
  • 43
  • 77
zatenzu
  • 307
  • 2
  • 10
  • If you are using Oracle JDK... But jvisualvm can connect to other JDK's processes as well, example with WebSphere described here: http://jcraane.blogspot.de/2012/01/monitoring-ibm-jvm-with-visualvm.html But the most interesting in that case, the Memory sampling (how much which objects are allocated) is not available. – Danubian Sailor Aug 27 '14 at 09:59
1

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.

Marcin Szawurski
  • 1,190
  • 10
  • 15
1

The amount of memory given to Java process is specified at startup. memory divided into separate areas, heap and permgen being the most familiar sub-areas.

While you specify the maximum size of the heap allowed for this particular process via -Xmx, the corresponding parameter for permgen is -XX:MaxPermSize. 90% of the Java apps seem to require between 64 and 512 MB of permgen to work properly. In order to find your limits, experiment a bit.

to solve this issue you have change your VM arguments

-Xms256m -Xmx1024m -XX:+DisableExplicitGC -Dcom.sun.management.jmxremote
-XX:PermSize=256m -XX:MaxPermSize=512m

add above two line in VM argument i am sure you will not face this problem any more

to know more about go to OutOfMemory

Nirav Prajapati
  • 2,828
  • 22
  • 32
1

Low memory configuration :-

It is possible that you have estimate less memory for your application for example your application need 2 Gb of memory but you have configured only 512 Mb so here you will get an OOME(Out-of-memory errors )

Due to Memoryleak :-

Memory leak is responsible for decreasing the available memory for heap and can lead to out of memory error for more read What is a Memory Leak in java?

Memory fragmentation :-

It is possible that there may be space in heap but it may be not contiguous . And heap needs compaction . Rearrange its memory.

Excess GC overhead :-

Some JVM implementations, such as the Oracle HotSpot, will throw an out-of-memory error when GC overhead becomes too great. This feature is designed to prevent near-constant garbage collection—for example, spending more than 90% of execution time on garbage collection while freeing less than 2% of memory. Configuring a larger heap is most likely to fix this issue, but if not you’ll need to analyze memory usage using a heap dump

Allocating over-sized temporary objects:-

Program logic that attempts to allocate overly large temporary objects. Since the JVM can’t satisfy the request, an out-of-memory error is triggered and the transaction will be aborted. This can be difficult to diagnose, as no heap dump or allocation-analysis tool will highlight the problem. You can only identify the area of code triggering the error, and once discovered, fix or remove the cause of the problem.

for more pls visit my site

http://all-about-java-and-weblogic-server.blogspot.com/2014/02/main-causes-of-out-of-memory-errors-in.html