性能文章>Java官方文档之了解OutOfMemoryError异常>

Java官方文档之了解OutOfMemoryError异常原创

2年前
235239

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.OutOfMemoryErrormay 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堆空间
Cause: The detail message Java heap space indicates object could not be allocated in the Java heap. This error does not necessarily imply a memory leak. The problem can be as simple as a configuration issue, where the specified heap size (or the default size, if it is not specified) is insufficient for the application.In other cases, and in particular for a long-lived application, the message might be an indication that the application is unintentionally holding references to objects, and this prevents the objects from being garbage collected. This is the Java language equivalent of a memory leak. Note: The APIs that are called by an application could also be unintentionally holding object references.One other potential source of this error arises with applications that make excessive use of finalizers. If a class has a 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
Cause: The detail message "GC overhead limit exceeded" indicates that the garbage collector is running all the time and Java program is making very slow progress. After a garbage collection, if the Java process is spending more than approximately 98% of its time doing garbage collection and if it is recovering less than 2% of the heap and has been doing so far the last 5 (compile time constant) consecutive garbage collections, then a 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.
原因:详细信息“GC开销限制超过”表示垃圾收集器一直在运行,Java程序进展非常缓慢。在垃圾收集后,如果Java进程花费了大约98%以上的时间进行垃圾收集,并且它正在恢复不到2%的堆,并且到目前为止一直在进行最后5个(编译时间常数)连续垃圾收集,则会引发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限制
Cause: The detail message "Requested array size exceeds VM limit" indicates that the application (or APIs used by that application) attempted to allocate an array that is larger than the heap size. For example, if an application attempts to allocate an array of 512 MB but the maximum heap size is 256 MB then 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).
原因:详细信息“请求的数组大小超过VM限制”表示应用程序(或该应用程序使用的API)试图分配大于堆大小的数组。例如,如果应用程序尝试分配512 MB的数组,但最大堆大小为256 MB,则将引发OutOfMemoryError,原因为请求的数组大小超过VM限制。操作:通常问题要么是配置问题(堆大小太小),要么是导致应用程序尝试创建大型数组的错误(例如,当使用计算错误大小的算法计算数组中的元素数量时)。
4,Exception in thread thread_name: java.lang.OutOfMemoryError: Metaspace
thread thread_name中的异常:java.lang.OutOfMemoryError:元空间
Cause: Java class metadata (the virtual machines internal presentation of Java class) is allocated in native memory (referred to here as metaspace). If metaspace for class metadata is exhausted, a 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.
原因:Java类元数据(Java类的虚拟机内部表示)分配在本机内存中(此处称为元空间)。如果类元数据的元空间已用尽,则会抛出带有MetaSpace详细信息的java.lang.OutOfMemoryError异常。可用于类元数据的元空间量受命令行上指定的参数MaxMetaSpaceSize的限制。当类元数据所需的本机内存量超过MaxMetaSpaceSize,将抛出带有详细信息MetaSpacejava.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:出于原因请求大小字节。掉期空间?
Cause: The detail message "request size bytes for reason. Out of swap space?" appears to be an 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:压缩类空间
Cause: On 64-bit platforms a pointer to class metadata can be represented by a 32-bit offset (with 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.
原因:在64位平台上,指向类元数据的指针可以用32位偏移量(使用UseCompressedOops)表示。这由命令行标志UseCompressedClassPointers(默认情况下打开)控制。如果使用UseCompressedClassPointers,则类元数据的可用空间量将固定在CompressedClassSpaceSize的量上。如果UseCompressedClassPointers所需的空间超过CompressedClassSpaceSize,则抛出带有详细信息压缩类空间的java.lang.OutOfMemoryError。操作:增加CompressedClassSpaceSize以关闭UseCompressedClassPointers注意:CompressedClassSpaceSize的可接受大小有界限。例如-XX: CompressedClassSpaceSize=4g,超过可接受的界限将导致消息,例如CompressedClassSpaceSize4294967296无效;必须在1048576和3221225472之间。
 
Note: There is more than one kind of class metadata - 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
Cause: If the detail part of the error message is "reason stack_trace_with_native_method" and a stack trace is printed in which the top frame is a native method, then this is an indication that a native method has encountered an allocation failure. The difference between this and the previous message is that the allocation failure was detected in a Java Native Interface (JNI) or native method rather than in the JVM code.
原因:如果错误消息的详细信息部分是“reason stack_trace_with_native_method”,并且打印了一个堆栈跟踪,其中顶部帧是本机方法,那么这表明本机方法遇到了分配失败。这与上一条消息的区别在于,分配失败是在Java本机接口(JNI)或本机方法中检测到的,而不是在JVM代码中。
 
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异常,您可能需要使用操作系统的本机实用程序来进一步诊断问题。有关可用于各种操作系统的工具的更多信息,请参阅本机操作系统工具
点赞收藏
金色梦想

终身学习。

请先登录,查看3条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步
9
3