Java OutOfMemoryError 时如何识别问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25519560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to identify the issue when Java OutOfMemoryError?
提问by Rahul Tripathi
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.
如何在 javaOutOfMemoryError
或stackoverflow
投入生产时识别问题。它是由于什么原因或服务器关闭的原因。
For example I am developing an application which is lived on production and UAT. Instantly on production java OutOfMemoryError
or stackoverflow
.
例如,我正在开发一个在生产和 UAT 上运行的应用程序。立即在生产 javaOutOfMemoryError
或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.
请解释一下。我多次遇到过这个问题。
采纳答案by Niels Bech Nielsen
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
让 VM 在 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.
并将其用于分析。内存分析器工具 ( http://eclipse.org/mat/) 是一个很好的独立程序,用于此分析。
回答by Rahul Tripathi
The Oracle docs:- Troubleshooting Memory Leakshas 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).
当在 Java 堆或堆的特定区域中没有足够的空间来分配对象时,会抛出此错误。垃圾收集器无法提供更多空间来容纳新对象,并且无法进一步扩展堆。
.....
诊断 OutOfMemoryError 的早期步骤是确定错误的含义。是说Java堆满了,还是本机堆满了?为了帮助您回答这个问题,以下小节解释了一些可能的错误消息,并参考消息的详细信息部分:
线程“main”中的异常:java.lang.OutOfMemoryError:Java 堆空间
请参阅 3.1.1 详细消息:Java 堆空间。
线程“main”中的异常:java.lang.OutOfMemoryError:PermGen space
参见 3.1.2 详细信息:永久代空间。
线程“main”中的异常:java.lang.OutOfMemoryError:请求的数组大小超过 VM 限制
请参阅 3.1.3 详细消息:请求的阵列大小超出 VM 限制。
线程“main”中的异常:java.lang.OutOfMemoryError:请求字节。交换空间不足?
请参阅 3.1.4 详细消息:请求字节。交换空间不足?
线程“main”中的异常:java.lang.OutOfMemoryError:(本机方法)
参见 3.1.5 详细消息:(本机方法)。
UPDATE:-
更新:-
You can downloadthe 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:
您可以从 OpenJDK下载HotSpot VM 源代码。如果你想监控和跟踪你的 Java 堆空间的内存占用,即年轻代和老年代空间是从你的 HotSpot VM 启用详细 GC。您可以在 JVM 启动参数中添加以下参数:
-verbose:gc –XX:+PrintGCDetails –XX:+PrintGCTimeStamps –Xloggc:<app path>/gc.log
回答by zatenzu
You can use jvisualvm
to manage your process at the runtime.
您可以使用jvisualvm
在运行时管理您的流程。
You can see memory, heap space, objects etc ...
您可以看到内存、堆空间、对象等...
This program is located in your bin
directory of your JDK
.
该程序位于bin
您的JDK
.
回答by Nirav Prajapati
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.
分配给 Java 进程的内存量在启动时指定。内存分为不同的区域,heap 和 permgen 是最熟悉的子区域。
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.
当您通过 -Xmx 指定此特定进程允许的最大堆大小时,permgen 的相应参数是 -XX:MaxPermSize。90% 的 Java 应用程序似乎需要 64 到 512 MB 的永久代才能正常工作。为了找到你的极限,尝试一下。
to solve this issue you have change your VM arguments
要解决此问题,您必须更改 VM 参数
-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
在 VM 参数中添加上面两行我相信你不会再遇到这个问题
to know more about go to OutOfMemory
要了解更多信息,请访问OutOfMemory
回答by Marcin Szawurski
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.
最好尝试在您可以自由调试的地方重现问题——在开发服务器上或在您的本地机器上。然后尝试调试,查找递归调用、堆大小以及正在创建的对象。不幸的是,在本地机器上重现 prod env(及其负载等)并不总是那么容易,因此找到此类错误的根本原因可能是一个挑战。
回答by hemant kurmi
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 )
您可能估计应用程序的内存较少,例如您的应用程序需要 2 Gb 的内存,但您仅配置了 512 Mb,因此在这里您将收到 OOME(内存不足错误)
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?
内存泄漏负责减少堆的可用内存,并可能导致更多读取的内存不足错误 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 :-
多余的 GC 开销:-
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
某些 JVM 实现(例如 Oracle HotSpot)会在 GC 开销变得太大时引发内存不足错误。此功能旨在防止近乎恒定的垃圾收集——例如,将超过 90% 的执行时间用于垃圾收集,而释放的内存不到 2%。配置更大的堆最有可能解决这个问题,但如果不是,您将需要使用堆转储来分析内存使用情况
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.
试图分配过大临时对象的程序逻辑。由于 JVM 无法满足请求,因此会触发内存不足错误并中止事务。这可能很难诊断,因为没有堆转储或分配分析工具会突出问题。您只能识别触发错误的代码区域,一旦发现,修复或消除问题的原因。
for more pls visit my site
更多请访问我的网站