This discussion is archived
1 2 3 4 Previous Next 57 Replies Latest reply: Jan 13, 2006 1:56 PM by 807590 RSS

How to catch OutOfMemoryError?

807590 Newbie
Currently Being Moderated
Hi all.
I don't know how to catch OutOfMemoryError.
Can anyone help me with this, please?

Many thanks in advance.
Miso
  • 1. Re: How to catch OutOfMemoryError?
    3004 Newbie
    Currently Being Moderated
    Hi all.
    I don't know how to catch OutOfMemoryError.
    You don't.
    Can anyone help me with this, please?
    No.
    Many thanks in advance.
    Miso
    While such a thing is possible it is rather useless.
  • 2. Re: How to catch OutOfMemoryError?
    796440 Guru
    Currently Being Moderated
    You can catch it just like you would any other exception, although there's not much reason to do so.

    You need to either give the VM more memory with the -Xmx command line option, or change your program to use less memory.
  • 3. Re: How to catch OutOfMemoryError?
    3004 Newbie
    Currently Being Moderated
    You can catch it just like you would any other
    exception,
    Throwable.
  • 4. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    In some extreme cases my application throws OutOfMemoryError. I want to inform the user that his/her RAM is not big enough to handle such big amount of data.
    Is this possible to do?
    thanks
  • 5. Re: How to catch OutOfMemoryError?
    3004 Newbie
    Currently Being Moderated
    In some extreme cases my application throws
    OutOfMemoryError. I want to inform the user that
    his/her RAM is not big enough to handle such big
    amount of data.
    Is this possible to do?
    thanks
    Probably not no.

    Because when the VM throws an OutOfMemoryError then it is a hurtin real bad. So creating new objects isn't going to help much.

    If the user can specify things that make your application blow up then you need to detect these conditions before the error occurs.
  • 6. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    If the user can specify things that make your
    application blow up then you need to detect these
    conditions before the error occurs.
    OK. So does it mean that the OutOfMemoryError should never be thrown, because it's not possible to do anything in this case because of leak of memory?
  • 7. Re: How to catch OutOfMemoryError?
    3004 Newbie
    Currently Being Moderated
    If the user can specify things that make your
    application blow up then you need to detect these
    conditions before the error occurs.
    OK. So does it mean that the OutOfMemoryError should
    never be thrown, because it's not possible to do
    anything in this case because of leak of memory?
    Hmmm. Well it means it should be thrown when it runs out of memory. It also means that you should not generally bother catching it.

    But yes if you are doing things right then you shouldn't have the problem in the first place. Mostly.

    It would be more helpful if you described what you are doing that is causing the problem.
  • 8. Re: How to catch OutOfMemoryError?
    796440 Guru
    Currently Being Moderated
    You can catch it just like you would any other
    exception,
    Throwable.
    exception, lowercase "e".
  • 9. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    Well, I have tried to google some articles and finally now I understand the problem.
    Many thanks to all who tried to help.
    Bye
  • 10. Re: How to catch OutOfMemoryError?
    3004 Newbie
    Currently Being Moderated
    You can catch it just like you would any other
    exception,
    Throwable.
    exception, lowercase "e".
    My point was that it is not a subclass of Exception. But it appears we failed anyway...
  • 11. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    In some extreme cases my application throws
    OutOfMemoryError. I want to inform the user that
    his/her RAM is not big enough to handle such big
    amount of data.
    Is this possible to do?
    thanks
    Probably not no.

    Because when the VM throws an OutOfMemoryError then
    it is a hurtin real bad. So creating new objects
    isn't going to help much.
    Well, that's a little too gloomy... Yes, you can catch the error and do something about it, as long as you keep your goals VERY modest.

    1) Catch it with "catch (Throwable)" or something more specific like "catch (Error)" or "catch (OutOfMemoryError)".

    2) If (AND ONLY IF) you catch this error high up in the application (say in main() or a Servlet method processing method (doGet(), doPost(), etc.), to of a thread or other such location, you should then might enough memory available to produce and display an error message. You might try to recover and continue (good luck, lots of issues there). You might try issuing the following in the catch clause to try and cleanup the heap before you do anything else, but there are no guarantees this will do anything:
       System.gc();
       System.runFinalization();
       System.gc();
    Most experience Java developers avoid any such effort and instead ensure that their JVM's are configured appropriately and failing that allow the JVM to puke and die.
  • 12. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    If the user can specify things that make your
    application blow up then you need to detect these
    conditions before the error occurs.
    OK. So does it mean that the OutOfMemoryError should
    never be thrown, because it's not possible to do
    anything in this case because of leak of memory?
    No. It means that if you mess up the Error will simply ensure that the JVM dies.
  • 13. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    System.gc();
    System.runFinalization();
    System.gc();
    IIRC, the JVM will always run Garbage Collection before throwing an OOME.
  • 14. Re: How to catch OutOfMemoryError?
    807590 Newbie
    Currently Being Moderated
    System.gc();
    System.runFinalization();
    System.gc();
    IIRC, the JVM will always run Garbage Collection
    before throwing an OOME.
    I believe it's required by the spec. I'm not sure how helpful calling .gc() and .runFinalization() might be but it's a last ditch effort to get enough memory to toss out a simple message so what the heck.
1 2 3 4 Previous Next