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

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

问题描述

文档 java.lang.Error 说:


错误是Throwable的子类,表明存在严重问题一个合理的应用程序不应该试图抓住

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

但是作为 java.lang.Error java.lang.Throwable 的子类,我可以捕获这种类型的Throwable。

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

我理解为什么抓住这种例外不是一个好主意。据我所知,如果我们决定捕获它,catch处理程序不应该自己分配任何内存。否则将再次抛出 OutOfMemoryError

I understand why it's not 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 ,我们怎么能确定catch处理程序本身不分配任何内存(任何工具或最好的)做法)?

  1. Is there any real word scenarios when catching java.lang.OutOfMemoryError may be a good idea?
  2. If we decide to catch java.lang.OutOfMemoryError, how can we sure that catch handler doesn't allocate any memory by itself (any tools or best practices)?


推荐答案

我同意并不同意这里的大部分回复。

I agree and disagree with most the responses here.

在许多情况下,您可能希望捕获 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 ,这是优雅地关闭,干净地释放资源并尽可能地记录失败的原因(如果是sti)可能会这样做。)

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.

当抛出错误时,堆包含相同的分配量对象不成功分配之前的对象,现在是时候删除对运行时对象的引用,以释放更多可能需要清理的内存。在这些情况下,它甚至可能继续,但这肯定是一个坏主意,因为你永远不能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

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

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.

需要注意的是,您必须仅在可能进行清理的地方捕获这些类型的错误。不要随身携带抓住(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天全站免登陆