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.