neizan
neizan

Reputation: 2321

How to detect when the Java heap is nearly full and prevent "OutOfMemoryError: Java Heap"?

There a quite a few questions in SO related to the "OutOfMemoryError: Java Heap" error, but reading over them, most seem to discuss how to increase the heap size or profiling the app and detecting memory leaks.

I'm working on a project, that invovles analyzing the cost of a branch and bound algorithm. For input small input sizes, the potential number of solutions to search grows at O(n!). At a certain input size, n, I've encountered the "OutOfMemoryError" because the parial solutions are kept in a priority queue until ready to be treated, and the huge number of partial solutions in the queue fills up the memory. So, I know I don't have a memory leak, and I don't necessarily want to increase the heap size.

What I'd like to do is simply detect when the memory is nearly full, then give the user a message that tells them what's going on and why the program is exiting (it's not necessary the program keep functioning at this point). Is there a way to do this? I have looked at the java.lang.management package, but it doesn't make much sense to me, and I've had difficulty finding decent example code. Any explanation or example code is appreciated.

Upvotes: 4

Views: 4368

Answers (5)

Marc Polizzi
Marc Polizzi

Reputation: 9375

You might want to look at this : http://docs.oracle.com/javase/6/docs/technotes/guides/management/mxbeans.html and more specifically the section related to memory "threshold".

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346327

The most simple and direct solution is to catch the OutOfMemoryError at a very high level of the application and then just show the message, then close the application.

In theory, this could fail as there might not be enough memory to show the message, but in practice this will almost never happen because a lot of objects go out of scope when the error is caught, giving you almost certainly enough for simple tasks.

Upvotes: 1

rsp
rsp

Reputation: 23373

You can get the memory status using the Runtime api;

        // Memory status
        Runtime runtime = Runtime.getRuntime();

        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();

        boolean memoryOK = minFree <= freeMemory;

Upvotes: 1

Thilo
Thilo

Reputation: 262534

What I'd like to do is simply detect when the memory is nearly full, then give the user a message that tells them what's going on and why the program is exiting (it's not necessary the program keep functioning at this point).

That's difficult. Mostly because free memory is not really known before a garbage collection takes place, and a serious garbage collection usually only happens just before you run out.

What you can do is explain why the program has crashed after the fact: Eclipse does this for example. You can catch the OutOfMemoryError just like any other Throwable, and show your message:

 try{
      heavyLifting();
 }
 catch (OutOfMemoryError e){
    showAMessage();   
 }

Upvotes: 6

Zohaib
Zohaib

Reputation: 7116

I dont think you can do much a part from making your code memory efficient. Even if you get to know that memory is about to full, and now out of memory error is about to come, there is very less that you can do, At best, you can request garbage collector to become active, but its a request. And rest assure, JVM will do its best to prevent out of memory error, If it still comes, most probably it was unavoidable with given code.

Upvotes: 0

Related Questions