什么是 OutOfMemoryError 以及如何调试和修复它 [英] What is an OutOfMemoryError and how do I debug and fix it

查看:29
本文介绍了什么是 OutOfMemoryError 以及如何调试和修复它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 程序抛出了 OutOfMemoryError.我该如何调试和解决这个问题?

My Java program threw an OutOfMemoryError. How do I debug and fix this problem?

许多 Java 新手都在努力应对 OutOfMemoryError.这是尝试创建一个规范问题,以回答有关 OutOfMemoryError 的最常见问题.我正在创建这个新问题,而不是调整之前关于 OutOfMemoryError 的众多问题之一,因为这些问题及其答案与一个人遇到的特定问题紧密相关.

Many newcomers to Java struggle to cope with an OutOfMemoryError. This is an attempt to create a canonical question that will answer the most frequently asked questions about an OutOfMemoryError. I'm creating this new question, rather than adapting one of the numerous previous questions about a OutOfMemoryError because those questions and their answers are tightly coupled to the particular problem a person had.

这个问题不同于 调试异常的一般建议 因为问题的原因并不总是在调用堆栈上,需要具体的建议.

This question is distinct from general advice on debugging exceptions because the cause of the problem is not always on the call stack, and specific advice is necessary.

推荐答案

OutOfMemoryError 是 Java 虚拟机 (JVM) 抛出的异常,因为它需要为(新)对象分配内存,但该对象的可用内存不足.JVM 将首先尝试释放死对象使用的内存,通过 运行垃圾收集器.

An OutOfMemoryError is an exception thrown by the Java Virtual Machine (JVM) because it needs to allocate memory for a (new) object, but insufficient memory is available for the object. The JVM will have first tried to free memory used by dead objects, by running the garbage collector.

由于 OutOfMemoryErrorVirtualMachineError,JVM 被允许 任何时间抛出它,尽管它必须先尝试通过垃圾收集释放内存.

As an OutOfMemoryError is a VirtualMachineError, the JVM is permitted to throw it at any time, although it must try to release memory through garbage collection first.

然而,在实践中,它可能会从试图创建无法为其分配内存的对象的 new 语句中抛出.因此,您应该首先检查与异常相关的堆栈跟踪,以获取有关问题原因的线索,就像处理任何其他异常一样.

However, in practice it is likely to be thrown from a new statement that tried to create an object for which memory could not be allocated. Therefore, you should first examine the stacktrace associated with the exception for clues about the cause of the problem, as you would for any other exception.

  • 如果在尝试分配数组时抛出异常(例如 int[] values = new int[n]),原因可能是您试图创建一个过大的数组数组(n 太大).你在计算你需要的数组大小时出错了吗?
  • 如果尝试在其他人编写的容器类的方法中分配数组而引发异常,则原因可能是您的代码要求容器存储过多的内容.ArrayList.reserve(int)HashMap(int) 等方法必须分配存储空间以备将来使用.您是否在计算您需要的容器尺寸时出错?
  • 如果异常是从循环内部抛出的,原因可能是代码循环次数过多.您的循环终止条件是否正确?如果是 for 循环,您是否要求它循环正确的次数?
  • If the exception is thrown from an attempt to allocate an array (such as int[] values = new int[n]), the cause could be that you are trying to create an excessively large array (n is too large). Have you made a mistake in the calculation of the size of array you need?
  • If the exception is thrown from an attempt to allocate an array in a method of a container class written by someone else, the cause could be that your code is asking the container to store an excessive number of things. Methods such as ArrayList.reserve(int) and HashMap(int) must allocate storage for future use. Have you made a mistake in the calculation of the size of container you need?
  • If the exception is thrown from inside a loop, the cause could be that the code has looped too many times. Is your loop termination condition correct? If it is a for loop, are you asking it to loop the correct number of times?

如果堆栈跟踪没有提供足够的线索,您可以尝试使用堆分析器.这是一个监视程序,它使您能够在程序运行时检查用于对象的内存,或者检查程序退出时写入的堆转储.它可以提供有关存储在内存中的对象的大小、数量和类别的信息.

If the stacktrace does not provide enough clues, you could try using a heap profiler. That is a monitoring program that enables you to examine the memory used for objects while the program runs, or examies a heap dump written when the program exits. It can provide information about the sizes, number and classes of objects stored in memory.

JVM 有一个 有限的内存量 提供给它.您可能会得出结论,您的程序运行正常,但只是需要比可用内存更多的内存来运行.如果你没有明确告诉 JVM 要使用多少内存,大多数实现会选择一个 合理的默认值 数量,但该数量对于您的程序来说可能太小了.JVM 的命令行选项可以控制可用的内存量.对于大多数 JVM 实现,这些选项中最重要的是 -Xmx-Xms.

The JVM has a finite amount of memory made available to it. You might conclude that you program is behaving correctly, but just needs more memory to run than has been made available to it. If you do not explicitly tell the JVM how much memory to use, most implementations will choose a sensible default amount based on the amount of RAM that your computer has, but that amount could be too small for your program. Command line options for the JVM can control how much memory is made available. For most JVM implementations, the most important of those options are -Xmx and -Xms.

这篇关于什么是 OutOfMemoryError 以及如何调试和修复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆