捕获 java.lang.OutOfMemoryError? [英] Catching java.lang.OutOfMemoryError?

查看:44
本文介绍了捕获 java.lang.OutOfMemoryError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档 java.lang.Error 说:

一个 Error 是 Throwable 的一个子类,它表示一个合理的应用程序不应该试图捕捉的严重问题

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch

但由于 java.lang.Errorjava.lang.Throwable 的子类,我可以捕获这种类型的 Throwable.

But as java.lang.Error is a subclass of java.lang.Throwable, I can catch this type of Throwable.

我理解为什么捕捉这种异常不是一个好主意.据我了解,如果我们决定捕获它,则捕获处理程序不应自行分配任何内存.否则 OutOfMemoryError 将再次抛出.

I understand why it's not a good idea to catch this sort of exception. As far as I understand, if we decide to catch it, the catch handler should not allocate any memory by itself. Otherwise OutOfMemoryError will be thrown again.

所以,我的问题是:

  1. 在捕捉 java.lang.OutOfMemoryError 可能是一个好主意时,是否有任何现实世界的场景?
  2. 如果我们决定捕获 java.lang.OutOfMemoryError,我们如何确保捕获处理程序不会自行分配任何内存(任何工具或最佳实践)?
  1. Are there any real world scenarios when catching java.lang.OutOfMemoryError might be a good idea?
  2. If we decide to catch java.lang.OutOfMemoryError, how can we make sure the catch handler doesn't allocate any memory by itself (any tools or best practices)?

推荐答案

在许多情况下,您可能希望捕获 OutOfMemoryError 并且根据我的经验(在 Windows 和 Solaris JVM 上),OutOfMemoryError 是 JVM 的丧钟.

There are a number of scenarios where you may wish to catch an OutOfMemoryError and in my experience (on Windows and Solaris JVMs), only very infrequently is OutOfMemoryError the death-knell to a JVM.

只有一个很好的理由可以捕获 OutOfMemoryError,那就是优雅地关闭、干净地释放资源并尽可能地记录失败的原因(如果仍然可以这样做的话)).

There is only one good reason to catch an OutOfMemoryError and that is to close down gracefully, cleanly releasing resources and logging the reason for the failure best you can (if it is still possible to do so).

一般来说,OutOfMemoryError 是由于块内存分配无法满足堆的剩余资源.

In general, the OutOfMemoryError occurs due to a block memory allocation that cannot be satisfied with the remaining resources of the heap.

Error 被抛出时,堆包含的分配对象数量与分配不成功之前相同,现在是时候删除对运行时对象的引用以释放更多内存清理所需.在这些情况下,甚至可以继续,但这绝对是一个坏主意,因为您永远无法 100% 确定 JVM 处于可修复状态.

When the Error is thrown the heap contains the same amount of allocated objects as before the unsuccessful allocation and now is the time to drop references to run-time objects to free even more memory that may be required for cleanup. In these cases, it may even be possible to continue but that would definitely be a bad idea as you can never be 100% certain that the JVM is in a reparable state.

证明OutOfMemoryError并不意味着JVM在catch块中内存不足:

Demonstration that OutOfMemoryError does not mean that the JVM is out of memory in the catch block:

private static final int MEGABYTE = (1024*1024);
public static void runOutOfMemory() {
    MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    for (int i=1; i <= 100; i++) {
        try {
            byte[] bytes = new byte[MEGABYTE*500];
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
            long maxMemory = heapUsage.getMax() / MEGABYTE;
            long usedMemory = heapUsage.getUsed() / MEGABYTE;
            System.out.println(i+ " : Memory Use :" + usedMemory + "M/" +maxMemory+"M");
        }
    }
}

此代码的输出:

1 : Memory Use :0M/247M
..
..
..
98 : Memory Use :0M/247M
99 : Memory Use :0M/247M
100 : Memory Use :0M/247M

如果运行一些关键的东西,我通常会捕捉到 Error,将它记录到 syserr,然后使用我选择的日志记录框架记录它,然后继续释放资源并以干净的方式关闭.可能发生的最坏情况是什么?无论如何,JVM 正在消亡(或已经死了)并且通过捕获 Error 至少有清理的机会.

If running something critical, I usually catch the Error, log it to syserr, then log it using my logging framework of choice, then proceed to release resources and close down in a clean fashion. What's the worst that can happen? The JVM is dying (or already dead) anyway and by catching the Error there is at least a chance of cleanup.

需要注意的是,您必须仅在可以清理的地方捕获这些类型的错误.不要到处乱说catch(Throwable t) {} 或类似的废话.

The caveat is that you have to target the catching of these types of errors only in places where cleanup is possible. Don't blanket catch(Throwable t) {} everywhere or nonsense like that.

这篇关于捕获 java.lang.OutOfMemoryError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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