Suppose there is a large Java application and I would like to gracefully shut it down if OutOfMemory error occurs. Now I see the following problems:
- Some components catch and “swallow” all
ThrowablesincludingOutOfMemory - If a thread does not catch
OutOfMemoryit exits and we do not know thatOutOfMemoryoccurred
Can I somehow register a “JVM-level” handler to catch any OutOfMemory, which may occur in the JVM?
They shouldn’t do that, I would fix them.
That I would fix as well.
You can register an UncaughtExceptionHandler.
The only option might be to trigger a script on an OOME which shuts down you program. This can be done from the command line.