Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jvm: is it possible to find out process is shutting down due to an OOM while in the shutdown hook?

I have a critical process running in java (1.6), with a registered shutdown hook. In some instance where I get a OOM issue (more details below about the issue), the process stops suddenly, I don't get any of my logs, my catch(Throable x) is not catching the exception.

But the shutdown hook works. So if there was a way to know that the process is going to shutdown due to some nasty OOM, I could log necessary info before exiting.

Is there a way to do this?

About the OOM: Not sure what is the exception because as I said it does not get caught. I know it's a OOM because I start the process with

-XX:+HeapDumpOnOutOfMemoryError

and I get a heap dump file. In other cases an exception is caught, and it's a ava.lang.OutOfMemoryError: GC overhead limit exceeded. But not sure it's always this case.

EDIT:

In case it is not clear: I am not trying to prevent the OOM as it can happen for valid reasons in some scenario, I just want to make sure it is clear in the app log files

My question is: is it possible to find out process is shutting down due to an OOM while in the shutdown hook?

I need to do this programatically and from the same process.

For now the best approach is see if it exists a heap dump file java_pid_pid of process_.hprof (I know the pid) with recent date and deduce there was an OOM. I guess I could try Runtime.getRuntime().freeMemory() and report the issue if the memory available is very low, but not sure how reliable is that, maybe when the process is shutting down it has already released much memory, the approach above is best I think.

like image 217
Persimmonium Avatar asked Nov 10 '10 15:11

Persimmonium


People also ask

What happens when JVM shuts down?

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.

How do you test a shutdown hook?

getRuntime(). removeShutdownHook(Thread) method. Its boolean return value indicates if the respective thread was a registered hook previously. Thus, one can test if the hook' run() method performs correctly as one part of a test.

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.

What is a shutdown hook in Java?

Shutdown hooks are called when the application terminates normally (when all threads finish, or when System. exit(0) is called).


1 Answers

OOMs are tricky because if JVM is out of memory it might not run exception handling code due to a new OOM being thrown.

Try setting default uncaught exception handler. It will catch all uncaught exceptions.

like image 164
Peter Knego Avatar answered Sep 22 '22 01:09

Peter Knego