10

I have a program that sometimes throw OOME, I understand that there is a flag in the JVM options that I can set and whenever a certain Error/Exception appears (such as OOME) it calls a script I wrote. The script will give the user a notification and will call a the program with a different argument so it won't get OOME again.

does anyone know how to set this flag? what is the JVM options I need to set? I looked everywhere on line and couldn't find the answer.

help me please! Thanks, Aye

slm
  • 12,534
  • 12
  • 87
  • 106
Aye
  • 101
  • 1
  • 3
  • 1
    type 'java -X' at the command prompt. You will get a list of all the extended options. Might help. – Sagar V Sep 29 '10 at 11:29

2 Answers2

14

-XX:OnOutOfMemoryError="cmd args;cmd args"

From: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#DebuggingOptions

slm
  • 12,534
  • 12
  • 87
  • 106
TuomasR
  • 2,256
  • 17
  • 28
-4

I found one suggestion in a forum: catch the OOME in your application (like directly in main, assuming you're single threaded) and do this in the catch handler:

catch (OutOfMemoryError not_again) {
  System.gc();
  System.runFinalization();
  System.gc();
  System.out.println("Your error message");
}

Cleaning up the heap might free just enough memory to produce a last error message before dying.

Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253
  • thanks, but the application is very sparse and I don't want to catch it inside I prefer to call .dat file from the JVM itself – Aye Sep 29 '10 at 11:45
  • 8
    one would think that the JVM would attempt a few rounds of GC before throwing an OOME itself; this isn't guaranteed to really do much of anything. – matt b Sep 29 '10 at 12:10