Behavior of a Java process in case of OutOfMemoryError(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 anError
and not anException
. This means that it's very unlikely to be caught anywhere: You should not try to catch anError
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 情况下的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 进程在 OutOfMemoryError 情况下的行为
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01