How to identify the issue when Java OutOfMemoryErr

2019-06-14 23:57发布

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.

6条回答
Explosion°爆炸
2楼-- · 2019-06-15 00:27

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.

查看更多
三岁会撩人
3楼-- · 2019-06-15 00:30

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.

查看更多
Lonely孤独者°
4楼-- · 2019-06-15 00:35

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
查看更多
手持菜刀,她持情操
5楼-- · 2019-06-15 00:42

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

查看更多
太酷不给撩
6楼-- · 2019-06-15 00:45

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

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-06-15 00:46

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.

查看更多
登录 后发表回答