Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Catch outofmemory exception

Thread view: 
Bruce Lee - 17 Jan 2006 18:23 GMT
Is it possible to catch java.lang.OutofMemoryException ?
Stefan Ram - 17 Jan 2006 18:27 GMT
>Is it possible to catch java.lang.OutofMemoryException ?

 Yes.
Bruce Lee - 17 Jan 2006 18:34 GMT
> >Is it possible to catch java.lang.OutofMemoryException ?
>
>   Yes.

go on..
Stefan Ram - 17 Jan 2006 18:40 GMT
>>>Is it possible to catch java.lang.OutofMemoryException ?
>>Yes.
>go on..

 The correct spelling is "java.lang.OutOfMemoryError".

 So one would write

try { /* ... */ }
catch( final java.lang.OutOfMemoryError e ){ /* ... */ }


Steve W. Jackson - 17 Jan 2006 18:52 GMT
> > >Is it possible to catch java.lang.OutofMemoryException ?
> >
> >   Yes.
>
> go on..

No, because there's no such exception.  There's OutOfMemoryError.  You
can attempt to catch it, though, since it's Throwable just as Exception
is.

Reading up on Java memory management issues, you'll find that it's
almost never feasible to really catch it.  By the time it's thrown, it's
likely that the situation is beyond repair and your JVM is going to
terminate.
Signature

Steve W. Jackson
Montgomery, Alabama

Joe Attardi - 17 Jan 2006 19:18 GMT
If there's a reasonable chance an OutOfMemoryError will occur, it may
make sense indeed to catch it. Steve's point is true -- if this gets
thrown, the situation is likely beyond repair. So the only practical
purpose of catching this error is to die gracefully, i.e. with a
friendly error message instead of an exception trace.
Stefan Ram - 17 Jan 2006 20:13 GMT
>So the only practical purpose of catching this error is to die
>gracefully, i.e. with a friendly error message instead of an
>exception trace.

 Here is a block trying to allocate components into a pool as
 long as possible (as much as memory is available). Eventually
 about 30 % of the pool then will be released again to have
 some memory for other purposes.

{ char[] buffer = new char[ 65535 ];
 try { while( true )pool.add( new Component() ); }
 catch( final java.lang.OutOfMemoryError error )
 { buffer = null; java.lang.System.gc();
   for( int i = 0; i <( int )( pool.size() * .3 ); ++i )
   pool.removeLastComponent(); java.lang.System.gc(); }; }
Daniel Dyer - 17 Jan 2006 20:45 GMT
>> So the only practical purpose of catching this error is to die
>> gracefully, i.e. with a friendly error message instead of an
[quoted text clipped - 11 lines]
>     for( int i = 0; i <( int )( pool.size() * .3 ); ++i )
>     pool.removeLastComponent(); java.lang.System.gc(); }; }

Some other thread could allocate just a few bytes at the wrong time  
causing an OutOfMemoryError that you don't catch.  You would probably have  
to provide a default uncaught exception handler  
(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setDefaultUncaught
ExceptionHandler(java.lang.Thread.UncaughtExceptionHandler
))  
to deal with this possibility.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Joe Attardi - 18 Jan 2006 05:13 GMT
>   Here is a block trying to allocate components into a pool as
>   long as possible (as much as memory is available). Eventually
>   about 30 % of the pool then will be released again to have
>   some memory for other purposes.

This does not look like a good idea. For starters, it looks like you
are using a Throwable to control the normal flow of your program. In
other words, you are depending on the Throwable (in this case,
OutOfMemoryError) to be thrown in order for your program to continue
normally. This is a Bad Thing.
Adam Maass - 18 Jan 2006 00:22 GMT
> Is it possible to catch java.lang.OutofMemoryException ?

<facetious>
 There ain't no such creature.
</facetious>

It is possible to catch java.lang.OutOfMemoryError (note: Error, not
Exception), but it is a profoundly bad idea to try it.

-- Adam Maass
Alun Harford - 18 Jan 2006 11:06 GMT
> > Is it possible to catch java.lang.OutofMemoryException ?
>
[quoted text clipped - 4 lines]
> It is possible to catch java.lang.OutOfMemoryError (note: Error, not
> Exception), but it is a profoundly bad idea to try it.

If you're running a pretty GUI application then I'd agree - the
OutOfMemoryError can occur in your GUI thread and then you're stuck. The
only reason to catch it is to die gracefully. However, in a server the #1
rule is "Don't die", so it's far from a bad idea to try it.

Alun Harford
Adam Maass - 19 Jan 2006 02:58 GMT
> "Adam Maass" <adam.nospam.maass@comcast.net> wrote in message
>> > Is it possible to catch java.lang.OutofMemoryException ?
[quoted text clipped - 10 lines]
> only reason to catch it is to die gracefully. However, in a server the #1
> rule is "Don't die", so it's far from a bad idea to try it.

The problem with trying to catch OutOfMemoryError is that it could occur
*anywhere* there's an allocation (ignorning for the time being that the
class also represents certain other hard limits imposed by the OS that have
been exhausted -- threads and graphics handles comes to mind). In
particular, in a multi-threaded application, we don't know that the thread
that gets an OutOfMemoryError will have the knowledge necessary to release
enough references to make things OK.

My first instinct is that I'd rather have a server application die with a
nice stack trace than limp along producing spurious results. I might be
convinced otherwise for certain situations...

-- Adam Maass
iamfractal@hotmail.com - 19 Jan 2006 08:37 GMT
"I'd rather have a server application die with a
nice stack trace than limp along producing spurious results"

Yes, and that's the point: how do you get this nice stack trace?
OutOfMemoryError does not print a nice stack trace by defualt.

If you want it, you're going to have to catch it, and manually dump it
to a file (presuming you don't want to squirt it onto a screen and hope
it's still there next day).

Catching OutOfMemoryError's, as has been said on this thread, is almost
invariably not an attempt to correct anything, just to perform
precisely this kind of post-mortum data.

Even for a GUI. Would you rather you application just die, or print,
"Out of memory," and then die when you click, "OK?" At least with, "Out
of memory," the user knows he should load fewer
files/analysis-branches/whatever.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
alexandre cartapanis - 18 Jan 2006 11:29 GMT
Bruce Lee a écrit :
> Is it possible to catch java.lang.OutofMemoryException ?

I personnaly catch StackOverflowError without any problem. So catching
Error is possible.

But the OutOfMemoryError can be thrown by almost anything, and thinking
that it could be throwned by an only method is wrong. Imagine that youre
method consumes almost all memory, and then an other thread of the VM
allocates a few bytes: the error will be thrown by the thread, not by
you're method. So catching such errors may be very hasardous.

Signature

Alexandre CARTAPANIS - Responsable Système et Réseau
Email alexandre.cartapanis@macymed.fr
Gsm. 06 72 07 51 55

Macymed SARL - 9 bvd Kraëmer 13014 Marseille France
Tél. 04 91 48 31 58 - Fax. 04 91 02 36 47
Web http://www.macymed.fr - Email info@macymed.fr

Bruce Lee - 18 Jan 2006 15:46 GMT
Bruce Lee a écrit :
> Is it possible to catch java.lang.OutofMemoryException ?
>
>>I personnaly catch StackOverflowError without any problem. So catching
>>Error is possible.

>>But the OutOfMemoryError can be thrown by almost anything, and thinking
>>that it could be throwned by an only method is wrong. Imagine that youre
>>method consumes almost all memory, and then an other thread of the VM
>>allocates a few bytes: the error will be thrown by the thread, not by
>>you're method. So catching such errors may be very hasardous.

--

That's the problem. It'd be difficult for me to pinpoint where the error
might occur so try catching everything would be a pain. The workaround I'm
using is to use 2 JVMs so in the event the program throws a OutOfMemoryError
then the other one will fire it back up again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.