Compilefile.this.compileThread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
Application.getDBHandler().setAutoCommit(false);
MIBParserUtils.getDefaultMibsMap();
compileSelectedFiles();
Application.getDBHandler().CommitTrans();
Application.getDBHandler().setAutoCommit(true);
}
}
catch(OutOfMemoryError exp) {
JOptionPane.showMessageDialog(null, "Compilation Stopped.. Insufficient Memory!!!");
CompileMib.this.compileThread.interrupt();
System.gc();
dispose();
NmsLogger.writeDebugLog(exp);
}
finally {
}
}
I tried to compile some files within a thread. The UI selects more than 200 files to compile. During compilation an OutOfMemoryError occurred due to in sufficient memory in Eclipse. I want to stop the thread and display a message box and dispose the compile window in my application. I wrote the below code but its not working. Can I catch the exception and handle it, or is there a better solution?
You can certainly catch an OOME. But successfully recovering is another thing entirely. This Answer discusses some of the issues: https://stackoverflow.com/a/1692421/139985.
Another thing to consider is that the OOME might be being thrown on a different thread:
The
compileSelectedFiles()method or one of the other methods could be doing the work on another thread and throwing OOME there.The OOME could be being thrown on one of Eclipse’s background threads.
In either case, that
catchobviously won’t catch it.It is worth noting that calling
System.gc()after an OOME is a waste of time. I can guarantee that it won’t release any memory that wouldn’t be released anyway. All you are doing is suggesting that the JVM waste time on something that won’t help. If you are lucky, the JVM will ignore the suggestion.My advice would be to just increase Eclipse’s heap size by altering the
-XmxJVM parameter in the eclipse.ini file.