Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Shutdownhook guaranteed to be execute if JVM crashes [duplicate]

Tags:

java

Possible Duplicate:
When Shutdown Hooks Break Bad

I want to know if shutdownhook is guaranteed to execute in the scenario if JVM crashes.

like image 364
kanojia_chandrashekhar Avatar asked Feb 05 '13 13:02

kanojia_chandrashekhar


People also ask

What happens when JVM crashes?

When the JRockit JVM crashes, it generates a binary crash file ( . core or . mdmp ). By default, the binary crash file contains a copy of the entire JVM process.

What is Shutdownhook in Java?

Shutdown Hooks are a special construct that allows developers to plug in a piece of code to be executed when the JVM is shutting down. This comes in handy in cases where we need to do special clean up operations in case the VM is shutting down.

What prevents JVM from shutting down?

The Runtime. halt() can stop the shutdown sequence that has been started: Only the Runtime. halt(), which terminates the JVM forcefully, can stop the started shutdown sequence, which also means that invoking the System. exit() method will not work within a shutdown hook.

How do I crash my Java Virtual Machine?

To crash a JVM, aside from JNI, you need to find a bug in the VM itself. An infinite loop just consumes CPU. Infinitely allocating memory should just cause OutOfMemoryError's in a well built JVM. This would probably cause problems for other threads, but a good JVM still should not crash.


1 Answers

Nope. From the docs:

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

(emphasis mine)

like image 157
nneonneo Avatar answered Oct 11 '22 17:10

nneonneo