catching an OutOfMemoryError

L

Luke

My application was throwing an OutOfMemoryError. While looking for the
problem I was surprised to find that I could not catch the error and
print the stack trace. My catch statement seemed to be ignored and I
would only see a "java.lang.OutOfMemoryError" logged to the System.out
stream.

I was allocating large chunks of memory, so when the error happened I
assumed that there would be enough memory left for my application to
handle the error. I'm still curious about the failure to catch the
error.
 
R

Roedy Green

My application was throwing an OutOfMemoryError. While looking for the
problem I was surprised to find that I could not catch the error and
print the stack trace. My catch statement seemed to be ignored and I
would only see a "java.lang.OutOfMemoryError" logged to the System.out
stream.

You'd think there would be a run time switch to let you reserve X
bytes out of the pool, then on out of memory, throw those back into
the pool and trigger the exception, giving you a tiny bit of breathing
room to deal with the exception.
 
E

ExGuardianReader

Luke said:
My application was throwing an OutOfMemoryError. While looking for the
problem I was surprised to find that I could not catch the error and
print the stack trace. My catch statement seemed to be ignored and I
would only see a "java.lang.OutOfMemoryError" logged to the System.out
stream.

I was allocating large chunks of memory, so when the error happened I
assumed that there would be enough memory left for my application to
handle the error. I'm still curious about the failure to catch the
error.

Try

catch (Throwable)
 
T

Thomas Hawtin

ExGuardianReader said:
Try

catch (Throwable)

That or the catch clause was itself throwing OOME from its own
allocations. An empty catch clause would prevent the error displaying,
and so provide confirmation that the exception was actually being
caught. An important consideration when handling OOME, is are you making
sure you are not going to throw again before it is handled.

Tom Hawtin
 
J

John C. Bollinger

Luke said:
My application was throwing an OutOfMemoryError. While looking for the
problem I was surprised to find that I could not catch the error and
print the stack trace. My catch statement seemed to be ignored and I
would only see a "java.lang.OutOfMemoryError" logged to the System.out
stream.

You may have been misunderstanding what you saw. You *can* catch
OutOfMemoryError, either explicitly or by catching Error or Throwable.
You should not, however, expect to get a stack trace out of one. The VM
creates an OOME instance at startup to throw at need, because if it
really does run out of memory and need to throw one then it may not at
that point have enough memory to create a fresh one. If, as you say,
"java.lang.OutOfMemoryError" was being printed then that probably *was*
the stack trace.
I was allocating large chunks of memory, so when the error happened I
assumed that there would be enough memory left for my application to
--^^^^^^^

You are unwise to write programs that depend on unfounded assumptions.
It will bite you. Moreover, if your assumption is reasonable but wrong
then it may only bite you infrequently -- such as only after you ship
the product.
handle the error. I'm still curious about the failure to catch the
error.

It sounds to me like the error was caught, and you just didn't recognize it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top