• <legend id='AaS3A'><style id='AaS3A'><dir id='AaS3A'><q id='AaS3A'></q></dir></style></legend>
    <tfoot id='AaS3A'></tfoot>

    1. <small id='AaS3A'></small><noframes id='AaS3A'>

          <bdo id='AaS3A'></bdo><ul id='AaS3A'></ul>
        <i id='AaS3A'><tr id='AaS3A'><dt id='AaS3A'><q id='AaS3A'><span id='AaS3A'><b id='AaS3A'><form id='AaS3A'><ins id='AaS3A'></ins><ul id='AaS3A'></ul><sub id='AaS3A'></sub></form><legend id='AaS3A'></legend><bdo id='AaS3A'><pre id='AaS3A'><center id='AaS3A'></center></pre></bdo></b><th id='AaS3A'></th></span></q></dt></tr></i><div id='AaS3A'><tfoot id='AaS3A'></tfoot><dl id='AaS3A'><fieldset id='AaS3A'></fieldset></dl></div>

        JVM 可以在不重新启动的情况下从 OutOfMemoryError 中恢复吗

        Can the JVM recover from an OutOfMemoryError without a restart(JVM 可以在不重新启动的情况下从 OutOfMemoryError 中恢复吗)
        <legend id='2Gjky'><style id='2Gjky'><dir id='2Gjky'><q id='2Gjky'></q></dir></style></legend>
          <tbody id='2Gjky'></tbody>

          <bdo id='2Gjky'></bdo><ul id='2Gjky'></ul>
          <i id='2Gjky'><tr id='2Gjky'><dt id='2Gjky'><q id='2Gjky'><span id='2Gjky'><b id='2Gjky'><form id='2Gjky'><ins id='2Gjky'></ins><ul id='2Gjky'></ul><sub id='2Gjky'></sub></form><legend id='2Gjky'></legend><bdo id='2Gjky'><pre id='2Gjky'><center id='2Gjky'></center></pre></bdo></b><th id='2Gjky'></th></span></q></dt></tr></i><div id='2Gjky'><tfoot id='2Gjky'></tfoot><dl id='2Gjky'><fieldset id='2Gjky'></fieldset></dl></div>
                <tfoot id='2Gjky'></tfoot>
              1. <small id='2Gjky'></small><noframes id='2Gjky'>

                  本文介绍了JVM 可以在不重新启动的情况下从 OutOfMemoryError 中恢复吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  1. Can the JVM recover from an OutOfMemoryError without a restart if it gets a chance to run the GC before more object allocation requests come in?

                  2. Do the various JVM implementations differ in this aspect?

                  My question is about the JVM recovering and not the user program trying to recover by catching the error. In other words if an OOME is thrown in an application server (jboss/websphere/..) do I have to restart it? Or can I let it run if further requests seem to work without a problem.

                  解决方案

                  It may work, but it is generally a bad idea. There is no guarantee that your application will succeed in recovering, or that it will know if it has not succeeded. For example:

                  • There really may be not enough memory to do the requested tasks, even after taking recovery steps like releasing block of reserved memory. In this situation, your application may get stuck in a loop where it repeatedly appears to recover and then runs out of memory again.

                  • The OOME may be thrown on any thread. If an application thread or library is not designed to cope with it, this might leave some long-lived data structure in an incomplete or inconsistent state.

                  • If threads die as a result of the OOME, the application may need to restart them as part of the OOME recovery. At the very least, this makes the application more complicated.

                  • Suppose that a thread synchronizes with other threads using notify/wait or some higher level mechanism. If that thread dies from an OOME, other threads may be left waiting for notifies (etc) that never come ... for example. Designing for this could make the application significantly more complicated.

                  In summary, designing, implementing and testing an application to recover from OOMEs can be difficult, especially if the application (or the framework in which it runs, or any of the libraries it uses) is multi-threaded. It is a better idea to treat OOME as a fatal error.

                  See also my answer to a related question:

                  EDIT - in response to this followup question:

                  In other words if an OOME is thrown in an application server (jboss/websphere/..) do I have to restart it?

                  No you don't have to restart. But it is probably wise to, especially if you don't have a good / automated way of checking that the service is running correctly.

                  The JVM will recover just fine. But the application server and the application itself may or may not recover, depending on how well they are designed to cope with this situation. (My experience is that some app servers are not designed to cope with this, and that designing and implementing a complicated application to recover from OOMEs is hard, and testing it properly is even harder.)

                  EDIT 2

                  In response to this comment:

                  "other threads may be left waiting for notifies (etc) that never come" Really? Wouldn't the killed thread unwind its stacks, releasing resources as it goes, including held locks?

                  Yes really! Consider this:

                  Thread #1 runs this:

                      synchronized(lock) {
                           while (!someCondition) {
                               lock.wait();
                           }
                      }
                      // ...
                  

                  Thread #2 runs this:

                      synchronized(lock) {
                           // do something
                           lock.notify();
                      }
                  

                  If Thread #1 is waiting on the notify, and Thread #2 gets an OOME in the // do something section, then Thread #2 won't make the notify() call, and Thread #1 may get stuck forever waiting for a notification that won't ever occur. Sure, Thread #2 is guaranteed to release the mutex on the lock object ... but that is not sufficient!

                  If not the code ran by the thread is not exception safe, which is a more general problem.

                  "Exception safe" is not a term I've heard of (though I know what you mean). Java programs are not normally designed to be resilient to unexpected exceptions. Indeed, in a scenario like the above, it is likely to be somewhere between hard and impossible to make the application exception safe.

                  You'd need some mechanism whereby the failure of Thread #1 (due to the OOME) gets turned into an inter-thread communication failure notification to Thread #2. Erlang does this ... but not Java. The reason they can do this in Erlang is that Erlang processes communicate using strict CSP-like primitives; i.e. there is no sharing of data structures!

                  (Note that you could get the above problem for just about any unexpected exception ... not just Error exceptions. There are certain kinds of Java code where attempting to recover from an unexpected exception is likely to end badly.)

                  这篇关于JVM 可以在不重新启动的情况下从 OutOfMemoryError 中恢复吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)

                        • <bdo id='ag7s1'></bdo><ul id='ag7s1'></ul>
                          <legend id='ag7s1'><style id='ag7s1'><dir id='ag7s1'><q id='ag7s1'></q></dir></style></legend>

                          <small id='ag7s1'></small><noframes id='ag7s1'>

                            <tfoot id='ag7s1'></tfoot>
                              <tbody id='ag7s1'></tbody>

                          1. <i id='ag7s1'><tr id='ag7s1'><dt id='ag7s1'><q id='ag7s1'><span id='ag7s1'><b id='ag7s1'><form id='ag7s1'><ins id='ag7s1'></ins><ul id='ag7s1'></ul><sub id='ag7s1'></sub></form><legend id='ag7s1'></legend><bdo id='ag7s1'><pre id='ag7s1'><center id='ag7s1'></center></pre></bdo></b><th id='ag7s1'></th></span></q></dt></tr></i><div id='ag7s1'><tfoot id='ag7s1'></tfoot><dl id='ag7s1'><fieldset id='ag7s1'></fieldset></dl></div>