Java from memory then exit

I have a piece of software that needs to parse large files. Limiting input or providing infinite memory is not an option, so I have to live with flying OOME. Since OOME only kills Thread, my software runs in some crap state.

Outside, everything looks fine, because the process is running, but inside it is the braindead.

I would like to put a fork on it. But how can I do this?

Catching OOME does not guarantee that the next line of code will be executed. e.g. System.exit (9). Thus, the JVM should notice OOME and destory itself.

Is their some vm option for this?

+2
source share
2 answers

When you get OOME, you may be very low in memory, and registration does not always work. One way is to have a shutdown method that holds some memory that it does not free until it is closed.

eg.

private static byte[] lastResort = new byte[256*1024]; public static void handleOOME(OutOfMemoryError oome) { lastResort = null; try { LOG.fatal("Dying after ", oome); } finally { System.exit(-1); } } 
+8
source

So, the JVM should notice OOME and destroy itself. Is this their vm option for this?

No. No.

However, I have to ask a question that you cannot catch OutOfMemoryError and immediately call System.exit() in the exception handler.

In practice, this should work. The only potential problem is that you called Runtime.setRunFinalizersOnExit(true) ... and even that is ignored if you exit with non-zero exit status.

(The caveat about catching OOME and other occasional Error exceptions is that the JVM may not be in a state of compliance to continue execution. But calling System.exit(nonzero) does not!)

+2
source

All Articles