Java 进程在 OutOfMemoryError 情况下的行为 [英] Behavior of a Java process in case of OutOfMemoryError

查看:39
本文介绍了Java 进程在 OutOfMemoryError 情况下的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 程序在获取 OutOfMemoryError.是否有任何定义的行为?进程会崩溃还是进入等待/睡眠状态?

What would be the behavior of a Java program on getting OutOfMemoryError. Is there any defined behavior? Will the process crash or would it go into wait/sleep state?

更新:如果我没有在我的代码中处理它?<​​/p>

Update: if I am not handling it in my code?

推荐答案

并且 OutOfMemoryError 的处理方式与任何其他异常一样:

And OutOfMemoryError is handled like any other exception:

  • 如果它被抓住了,就不会再发生任何事情了.
  • 如果没有被捕获,那么 线程线程组 未捕获的异常处理程序处理它.这几乎总是会导致线程被停止.
  • If it is caught, then nothing more happens.
  • If it is not caught, then either the threads or the threads groups uncaught exception handler handles it. This pretty much always leads to the thread being stopped.

然而有两个因素在其他例外中并不真正存在:

However there are two factors that are not really there in other exceptions:

  • OutOfMemoryError错误 而不是 Exception.这意味着它不太可能在任何地方都被捕获:一般情况下,您不应该尝试捕获 Error(只有极少数例外),而且通常不会这样做,因此很有可能它被处理的相当低.
  • OutOfMemoryError 发生并且没有对象因此而符合 GC 条件时,那么您将仍然剩下很少的内存,并且您很可能会遇到稍后再次出现完全相同的问题.
  • OutOfMemoryError is an Error and not an Exception. This means that it's very unlikely to be caught anywhere: You should not try to catch an Error generally (with very few exceptions) and it's not usually done, so the chances of it being handled are rather low.
  • When an OutOfMemoryError happens and no object become eligible for GC because of that, then you'll still have little memory left and chances are that you'll run into the exact same problem again later on.

如果发生这种情况的线程是唯一的非守护线程(通常但不一定是执行 main 方法的主线程),那么该线程被杀死会导致整个 JVM 关闭(通常被视为崩溃").

And if the thread this happens to is the only non-daemon thread (often, but not necessarily, that's the main thread, that executes the main method), then that thread getting killed results in the whole JVM shutting down (which is often perceived as "a crash").

所以 tl;dr 是:它会可能杀死线程,如果内存问题没有解决,那么这可能会发生在越来越多的线程上.

So the tl;dr is: It will probably kill the thread, and if the memory-issue is not solved, then this can happen to more and more threads.

这篇关于Java 进程在 OutOfMemoryError 情况下的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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