捕捉OutOfMemoryError [英] Catching OutOfMemoryError

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

问题描述

在Java中是否有任何捕捉内存不足错误(java.lang.OutOfMemoryError)的地方?

Is there any point catching an out of memory error (java.lang.OutOfMemoryError) in Java?

推荐答案

.以下是一些可能有意义的示例:

Yes. Here are a few examples where it could make sense:

  • 如果您想通过正常关闭程序
  • 来处理它
  • 如果您想向用户显示问题或记录错误
  • 根据您的设计,您甚至可以清理内存并恢复工作状态
  • if you want to handle it by gracefully closing your program
  • if you want to display the problem to the user or log the error
  • depending on your design, you might even be able to clear up memory and restore a working state

但是,请注意,通常情况下(除非您处于一次要分配大量内存的位置),对于这些情况,您可能不会特别捕获OutOfMemoryError,而是在主入口点的顶部一直执行catch Throwable.

However, note that normally (unless you're at a spot where you'll be allocating tons of memory at once), you probably wouldn't specifically catch OutOfMemoryError for these cases, but rather do a catch Throwable all the way at the top in your main entry point.

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

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