Java官方文档之了解OutOfMemoryError异常原创
One common indication of a memory leak is the java.lang.OutOfMemoryError
exception. Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further. Also, this error may be thrown when there is insufficient native memory to support the loading of a Java class. In a rare instance, a java.lang.OutOfMemoryError
may be thrown when an excessive amount of time is being spent doing garbage collection and little memory is being freed.
内存泄漏的一个常见迹象是java.lang.OutOfMemoryError
异常。通常,当Java堆中分配对象的空间不足时,会引发此错误。在这种情况下,垃圾收集器无法提供容纳新对象的空间,堆也无法进一步扩展。此外,当本机内存不足以支持加载Java类时,可能会引发此错误。在极少数情况下,当花费过多时间进行垃圾收集且释放内存很少时,可能会引发java.lang.OutOfMemoryError
。
When a java.lang.OutOfMemoryError
exception is thrown, a stack trace is also printed.
当抛出java.lang.OutOfMemoryError
异常时,也会打印堆栈跟踪。
The java.lang.OutOfMemoryError
exception can also be thrown by native library code when a native allocation cannot be satisfied (for example, if swap space is low).
当无法满足本机分配时,也可以由本机库代码引发java.lang.OutOfMemoryError
异常(例如,如果交换空间较低)。
An early step to diagnose an OutOfMemoryError
exception is to determine the cause of the exception. Was it thrown because the Java heap is full, or because the native heap is full? To help you find the cause, the text of the exception includes a detail message at the end, as shown in the following exceptions.
诊断OutOfMemoryError
异常的早期步骤是确定异常的原因。抛出它是因为Java堆满了,还是因为原生堆满了?为了帮助您找到原因,异常文本在末尾包含一条详细信息,如下异常所示。
- 1,Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space
- thread thread_name中的异常:java.lang.OutOfMemoryError:Java堆空间
finalize
method, then objects of that type do not have their space reclaimed at garbage collection time. Instead, after garbage collection, the objects are queued for finalization, which occurs at a later time. In the Oracle Sun implementation, finalizers are executed by a daemon thread that services the finalization queue. If the finalizer thread cannot keep up, with the finalization queue, then the Java heap could fill up and this type of OutOfMemoryError
exception would be thrown. One scenario that can cause this situation is when an application creates high-priority threads that cause the finalization queue to increase at a rate that is faster than the rate at which the finalizer thread is servicing that queue.
-
Action: You can find more information about how to monitor objects for which finalization is pending in Monitor the Objects Pending Finalization
- 原因:详细信息Java堆空间表明无法在Java堆中分配对象。此错误并不一定意味着内存泄漏。问题可能很简单,就像配置问题一样简单,指定的堆大小(或默认大小,如果没有指定)对应用程序来说是不够的。在其他情况下,特别是对于寿命较长的应用程序,消息可能表明应用程序无意中持有对对象的引用,这防止了对象被垃圾收集。这是相当于内存泄漏的Java语言。注意:应用程序调用的API也可能无意中保留对象引用。这种错误的另一个潜在来源是过度使用终结器的应用程序。如果类有
finalize
方法,则该类型的对象在垃圾收集时不会被回收空间。相反,在垃圾收集后,对象会排队等待最终确定,这种情况会在稍后发生。在Oracle Sun实现中,终结器由一个守护进程线程执行,该线程为终结进程队列提供服务。如果finalizer线程无法跟上,则使用finalization队列,则Java堆可以填充,并引发此类OutOfMemoryError
异常。可能导致这种情况的一种情况是,应用程序创建高优先级线程,导致最终化队列以比最终化器线程为该队列提供服务的速度更快的速度增加。 -
操作:您可以在“监视待完成对象”中找到有关如何监控待完成对象的更多信息。
- 2,Exception in thread thread_name: java.lang.OutOfMemoryError: GC Overhead limit exceeded
- thread thread_name中的异常:java.lang.OutOfMemoryError:GC Overhead limit exceeded
java.lang.OutOfMemoryError
is thrown. This exception is typically thrown because the amount of live data barely fits into the Java heap having little free space for new allocations.Action: Increase the heap size. The java.lang.OutOfMemoryError
exception for GC Overhead limit exceeded can be turned off with the command line flag -XX:-UseGCOverheadLimit
.java.lang.OutOfMemoryError
。之所以引发此异常,是因为实时数据的数量几乎不适合Java堆,几乎没有空闲空间用于新的分配。行动:增加堆大小。超过GC开销限制的java.lang.OutOfMemoryError
异常可以通过命令行标志-XX:-UseGCOverheadLimit
关闭。- 3,Exception in thread thread_name: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
- thread thread_name中的异常:java.lang.OutOfMemoryError:请求的数组大小超过VM限制
OutOfMemoryError
will be thrown with the reason Requested array size exceeds VM limit.Action: Usually the problem is either a configuration issue (heap size too **all), or a bug that results in an application attempting to create a huge array (for example, when the number of elements in the array is computed using an algorithm that computes an incorrect size).OutOfMemoryError
,原因为请求的数组大小超过VM限制。操作:通常问题要么是配置问题(堆大小太小),要么是导致应用程序尝试创建大型数组的错误(例如,当使用计算错误大小的算法计算数组中的元素数量时)。- 4,Exception in thread thread_name: java.lang.OutOfMemoryError: Metaspace
- thread thread_name中的异常:java.lang.OutOfMemoryError:元空间
java.lang.OutOfMemoryError
exception with a detail MetaSpace
is thrown. The amount of metaspace that can be used for class metadata is limited by the parameter MaxMetaSpaceSize
, which is specified on the command line. When the amount of native memory needed for a class metadata exceeds MaxMetaSpaceSize
, a java.lang.OutOfMemoryError
exception with a detail MetaSpace
is thrown.Action: If MaxMetaSpaceSize
, has been set on the command-line, increase its value. MetaSpace
is allocated from the same address spaces as the Java heap. Reducing the size of the Java heap will make more space available for MetaSpace
. This is only a correct trade-off if there is an excess of free space in the Java heap. See the following action for Out of swap space detailed message.MetaSpace
详细信息的java.lang.OutOfMemoryError
异常。可用于类元数据的元空间量受命令行上指定的参数MaxMetaSpaceSize
的限制。当类元数据所需的本机内存量超过MaxMetaSpaceSize
,将抛出带有详细信息MetaSpace
的java.lang.OutOfMemoryError
异常。操作:如果在命令行上设置了MaxMetaSpaceSize
,请增加其值。MetaSpace
从与Java堆相同的地址空间分配。缩小Java堆的大小将为MetaSpace
提供更多空间。只有当Java堆中空闲空间过剩时,这是一个正确的权衡。有关“掉期空间”详细信息,请参阅以下操作。- 5,Exception in thread thread_name: java.lang.OutOfMemoryError: request size bytes for reason. Out of swap space?
- thread thread_name中的异常:java.lang.OutOfMemoryError:出于原因请求大小字节。掉期空间?
OutOfMemoryError
exception. However, the Java HotSpot VM code reports this apparent exception when an allocation from the native heap failed and the native heap might be close to exhaustion. The message indicates the size (in bytes) of the request that failed and the reason for the memory request. Usually the reason is the name of the source module reporting the allocation failure, although sometimes it is the actual reason.OutOfMemoryError
异常。然而,当本机堆的分配失败且本机堆可能接近耗尽时,Java HotSpot VM代码会报告此明显的异常。消息指示失败的请求的大小(以字节为单位)和内存请求的原因。通常原因是报告分配失败的源模块的名称,尽管有时这是实际原因。
-
Action: When this error message is thrown, the VM invokes the fatal error handling mechani** (that is, it generates a fatal error log file, which contains useful information about the thread, process, and system at the time of the crash). In the case of native heap exhaustion, the heap memory and memory map information in the log can be useful. For more information about understanding the fatal error log file, see Appendix A.
操作:抛出此错误消息时,VM调用致命错误处理机制(即生成致命错误日志文件,该文件包含崩溃时有关线程、进程和系统的有用信息)。在原生堆耗尽的情况下,日志中的堆内存和内存映射信息可能会有用。有关了解致命错误日志文件的更多信息,请参阅附录A。
If this type of the
OutOfMemoryError
exception is thrown, you might need to use troubleshooting utilities on the operating system to diagnose the issue further. For more information about tools available for various operating systems, see Native Operating System Tools.如果引发此类
OutOfMemoryError
异常,您可能需要使用操作系统上的故障诊断实用程序来进一步诊断问题。有关可用于各种操作系统的工具的更多信息,请参阅本机操作系统工具。
- 6,Exception in thread thread_name: java.lang.OutOfMemoryError: Compressed class space
- thread thread_name中的异常:java.lang.OutOfMemoryError:压缩类空间
UseCompressedOops
). This is controlled by the command line flag UseCompressedClassPointers
(on by default). If the UseCompressedClassPointers
is used, the amount of space available for class metadata is fixed at the amount CompressedClassSpaceSize
. If the space needed for UseCompressedClassPointers
exceeds CompressedClassSpaceSize
, a java.lang.OutOfMemoryError
with detail Compressed class space is thrown.Action: Increase CompressedClassSpaceSize
to turn off UseCompressedClassPointers
. Note: There are bounds on the acceptable size of CompressedClassSpaceSize
. For example -XX: CompressedClassSpaceSize=4g
, exceeds acceptable bounds will result in a message such asCompressedClassSpaceSize
of 4294967296 is invalid; must be between 1048576 and 3221225472.UseCompressedOops
)表示。这由命令行标志UseCompressedClassPointers
(默认情况下打开)控制。如果使用UseCompressedClassPointers
,则类元数据的可用空间量将固定在CompressedClassSpaceSize
的量上。如果UseCompressedClassPointers
所需的空间超过CompressedClassSpaceSize
,则抛出带有详细信息压缩类空间的java.lang.OutOfMemoryError
。操作:增加CompressedClassSpaceSize
以关闭UseCompressedClassPointers
。注意:CompressedClassSpaceSize
的可接受大小有界限。例如-XX: CompressedClassSpaceSize=4g
,超过可接受的界限将导致消息,例如CompressedClassSpaceSize
4294967296无效;必须在1048576和3221225472之间。klass
metadata and other metadata. Only klass
metadata is stored in the space bounded by CompressedClassSpaceSize
. The other metadata is stored in Metaspace
.klass
元数据和其他元数据。只有klass
元数据存储在CompressedClassSpaceSize
的边界空间中。其他元数据存储在Metaspace
中。- 7,Exception in thread thread_name: java.lang.OutOfMemoryError: reason stack_trace_with_native_method
- thread thread_name中的异常:java.lang.OutOfMemoryError:reason_trace_with_native_method
-
Action: If this type of the
OutOfMemoryError
exception is thrown, you might need to use native utilities of the OS to further diagnose the issue. For more information about tools available for various operating systems, see Native Operating System Tools.操作:如果抛出此类OutOfMemoryError
异常,您可能需要使用操作系统的本机实用程序来进一步诊断问题。有关可用于各种操作系统的工具的更多信息,请参阅本机操作系统工具。