3
 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?

Jesper
  • 202,709
  • 46
  • 318
  • 350
Nikhil
  • 2,857
  • 9
  • 34
  • 58
  • Once you reach OOM state, there's no guarantee on the state of the application. All bets are off. – Frank Pavageau Sep 26 '12 at 10:04
  • Can't you set more memory for eclipse jvm in eclipse.ini? – kamuflage661 Sep 26 '12 at 10:05
  • can i handle the exception in catch block? – Nikhil Sep 26 '12 at 10:07
  • You can handle OOME provided there is some free memory. Try adding `byte[] bytes = new byte[64*1024];` inside the try/catch block. – Peter Lawrey Sep 26 '12 at 10:09
  • This code breaks the Sun Java coding standards. It's harder to read as a result. Why are you storing source code in a database and compiling it at run time? What does this design save you? Why not just compile everything once and let the class loader bring in .class as needed? Crazy. – duffymo Sep 26 '12 at 10:14

3 Answers3

1

can i handle the exception in catch block?

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 catch obviously 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 -Xmx JVM parameter in the eclipse.ini file.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

There is almost always no reliable way to recover from OOM, as anything that you try to put into catch block can require more memory, which is not available. And GC has already tried its best before OOM is thrown, so no point in asking him again.

As always, you can either increase the amount of memory available to your application via Xmx option, or fix your application for it not to require that much memory.

One more possible source of error is memory leak. In this case there is only 1 course of action: find it and fix it. Plumbr can help in that.

Nikem
  • 5,716
  • 3
  • 32
  • 59
0

Have you tried adding the following to your eclipse.ini (located in same folder as eclipse.exe):

-Xmx1024m

This increases the heap space available to Eclipse. If your issue is during compilation, this may solve it. It gives 1GB of memory as heap space limit. Try -Xmx512m if you don't want to allocate quite so much space though.

Ren
  • 1,111
  • 5
  • 15
  • 24