java内存不行然后退出 [英] java out of memory then exit

查看:215
本文介绍了java内存不行然后退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须分析大文件的软件。限制输入或给予无限记忆不是一种选择,所以我必须忍受飞行的OOME。因为OOME只杀死了线程,所以我的软件运行得很糟糕。

I have a piece of software that has to analyze big files. Constraining the input or giving infinite memory is not an option, so I have to live with flying OOMEs. Because the OOME only kills the Thread, my software runs in some crappy state.

从外面看,一切看起来都没问题,因为这个过程正在运行,但内部是脑死亡。

From outside everything looks okay because the process is running but on the inside it's braindead.

我想拔掉插头。但是我怎么能这样做?

I would like to pull the plug on it. But how can I do this?

捕获OOME不保证下一行代码将被执行。例如System.exit(9)。所以JVM必须注意到OOME和destory本身。

Catching a OOME does not guranteee that the next line of code will be executed. e.g System.exit(9). So the JVM has to notice OOME and destory itself.

这是他们的一些vm选项吗?

Is their some vm option for this?

推荐答案

当你得到一个OOME时,你的内存可能非常低,并且日志记录并不总是有效。解决这个问题的一种方法是使用一个关闭方法来保存一些在关闭之前不会释放的内存。

When you get an OOME you can be very low on memory and logging doesn't always work. One way around this is to have a shutdown method which holds some memory it doesn't release until its shutting down.

例如

 private static byte[] lastResort = new byte[256*1024];
 public static void handleOOME(OutOfMemoryError oome) {
     lastResort = null;
     try {
        LOG.fatal("Dying after ", oome);
     } finally {
        System.exit(-1);
     }
  }

这篇关于java内存不行然后退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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