Se si utilizza OpenJDK, si può essere sicuri quando si eseguirà il comando impostato da -XX: opzione OnOutOfMemoryError.
Codice preso dal codice sorgente OpenJDK. Vedere: debug.cpp
void report_java_out_of_memory(const char* message) {
static jint out_of_memory_reported = 0;
// A number of threads may attempt to report OutOfMemoryError at around the
// same time. To avoid dumping the heap or executing the data collection
// commands multiple times we just do it once when the first threads reports
// the error.
if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {
// create heap dump before OnOutOfMemoryError commands are executed
if (HeapDumpOnOutOfMemoryError) {
tty->print_cr("java.lang.OutOfMemoryError: %s", message);
HeapDumper::dump_heap_from_oome();
}
if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
VMError err(message);
err.report_java_out_of_memory();
}
}
}
Nel caso in cui una breve spiegazione:
- Prima di tutto verifica se l'opzione è stata impostata HeapDumpOnOutOfMemoryError. In tal caso dump_heap_from_oome run()
- Sencondly se l'opzione è stata impostata OnOutOfMemoryError, report_java_out_of_memory eseguire()
Quindi, di sicuro se si utilizza il processo di OpenJDK sarà il dump della memoria e quindi chiudere.
Non pratico uccidere. Avere un try-catch di livello superiore e fare un syste.exit. –