4

How good is a try catch at catching out of memory exceptions? I don't have any experience writing software that at the low level manages its own memory, but I could imagine an approach to doing that.

I don't know the way Java actually handles memory exceptions. Is it possible that the program managing memory runs out of memory? When could I try to catch an out of memory exception and it would fail to catch the exception?

Thanks!

Carlos Bribiescas
  • 4,197
  • 9
  • 35
  • 66
  • http://stackoverflow.com/questions/1692230/is-it-possible-to-catch-out-of-memory-exception-in-java?rq=1 – NeilA Feb 08 '14 at 07:23
  • That is definitely along the same lines as what i asked, but i would like to know about specific cases that are vulnerable to not being caught by Out of memory exceptions gracefully. – Carlos Bribiescas Feb 08 '14 at 07:28

5 Answers5

2

You do not have to worry about any implicit allocations that happen as part of catching the throwable, per se. It is always possible to catch them. The JVM even keeps pre-allocated instances of OOM errors available for when trouble happens, just so that they themselves never fail to allocate.

However, there may well be secondary concerns:

  • Any allocation could be the proverbial straw that breaks the camel's back, so you likely won't know just where your code is going to throw an OOM error. It could even happen in a completely different thread from the one you're doing your memory-consuming work in, thus crashing completely different parts of the JVM.
  • Depending on just what you're going to do when you catch it, you may be allocating more memory (such as a LogRecord or StringBuilder, the latter of which could even happen implicitly as part of syntactical string concatenation), which could run out of memory again.

These concerns only apply if you're running out of memory "the normal way", however; that is, by allocating lots of "normal" objects. In contrast, if the operation running out of memory is, for instance, the single allocation of, say, a 10 GB array, then they do not pose a problem.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
1

How good is a try catch at catching out of memory exceptions?

It works just fine at one level ... but at another level it can be risky, and / or futile. See below.

Is it possible that the program managing memory runs out of memory?

The "program" managing memory is the garbage collector. AFAIK, it always has enough memory for its own purposes. Though if it didn't it would have no alternative but to hard crash the JVM.

When could I try to catch an out of memory exception and it would fail to catch the exception?

Only if the JVM crashes. Or, if the OOME is thrown on a different thread stack to the one where you are trying to catch it.


OK, so why is catching OOME risky and / or futile.

The first reason is that an OOME can be thrown on any thread stack without any warning. When you catch the exception, the handler (typically) has no way to know what was happening "up stack", and exactly how it failed. Hence, it has no way of knowing if the application's execution state has been damaged or compromised. (Was the thread in the middle of updating something important? Was it about to notify some other thread ... and the thread will now never get the notification?)

The second reason is that OOMEs are often a result of a Java storage leak ... caused by something "hang onto" objects when it shouldn't do. If you catch an OOME and attempt to resume ... when the problem was caused by a leak ... the chances are that the offending objects will still be reachable, and another OOME will follow pretty soon. In other words, your application could get stuck in a state where it is continually throwing and recovering from OOMEs. At best this is going to kill performance ... because the last thing that the JVM typically does before an OOME is to perform a full (stop the world) garbage collection. And that takes a significant time.

Note that is not to say that you should never catch OOMEs. Indeed, catching an OOME, reporting it and then shutting down is often a good strategy.

No, the thing that is risky / futile is to catch the OOME and then attempt to recover and continue running.

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

In java Out Of Memory is not an exception it is an error. You can do anything at program level but it is a system limitation. You can escape it by increasing default heap memory size.

export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"
Helios
  • 851
  • 2
  • 7
  • 22
0

An OutOfMemory is an error not an exception. Errors are specifically designed to diferenciate things that can be catch, and hopefully recover, by your program to low level errors that are non recoverable.

You only catch some thing if you think that something can be done to recover from this exception/error, you can do nothing to recover from a OutOfMemory.

AlfredoCasado
  • 818
  • 5
  • 7
-4


First of all,out of memory is Error in java not an Exception.We can handle only exceptions in java by using try-catch construct or throws clause.

Both Error and Exception classes extends Throwable class.But Error is irrecoverable condition and Exception is hand able.

For your situation ,go through Garbage Collection in java .

  • 2
    This is not true. All `Throwable`s are catchable, and Errors are such. – Dolda2000 Feb 08 '14 at 07:32
  • Only by convention is an exception handled, and only by convention should one leave errors alone. You could `catch(Throwable t)` and just *not care*, which would be terrifying. – Makoto Feb 08 '14 at 07:35
  • @Dolda2000 please give some example otherwise i am not agreed with you.. – iCodeCademy Feb 08 '14 at 07:36
  • 1
    Just try it yourself: `try {throw new OutOfMemoryError();} catch(OutOfMemoryError e) {System.out.println("Caught!");}` – Dolda2000 Feb 08 '14 at 07:38
  • Just for the record, the actual difference between Exceptions and Errors is that Errors are not *normally meant* to be caught, while Exceptions are. – Dolda2000 Feb 08 '14 at 07:40
  • @Dolda2000 Thanks.For telling this precious concept. – iCodeCademy Feb 08 '14 at 07:43
  • But what if this Error gendered by JVM ? – iCodeCademy Feb 08 '14 at 07:44
  • @iCodeCademy: That makes no difference. I have myself caught such errors as `NoClassDefFoundError` when using external libraries that may or may not be present. – Dolda2000 Feb 08 '14 at 07:57
  • @iCodeCademy you may want to update your answer so that it isn't wrong anymore – Erwin Bolwidt Feb 08 '14 at 08:13
  • Actually, every part of this answer is wrong (Garbage collection is automatic; if an OOMError is thrown then the JVM has already tried garbage collecting but it was still out of memory). I would just delete it. – Vitruvie Feb 08 '14 at 23:34