• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in thread "main" java.lang.OutOfMemoryError

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When getting this kind of an error, out of memory, is there a way to do something before exiting the program?

I tried wrapping the abending function with a try & catch, hoping that i could print some statistics before exiting, but it didn't seem to activate!

I know there is something like the last() function with threads, that will always be executed even if abending, but is there a similar function for regular programs?

Thanks very much for any insights! :roll:
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using JDK 5 you can set the thread's uncaught exception handler using:

Thread.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

If you are in in JDK 4 you can create a custom ThreadGroup which overrides the uncaughtException(Thread t, Throwable e) and make your threads part of that group.

However, in jDK 4, that technique would exclude threads created directly by the JVM like the main thread and the AWT-Event Dispatch Thread.

To catch those exceptions caused in the AWT-Event Dispatch thread, in JKD 4, you must set a system property :

System.setProperty("sun.awt.exception.handler", "ErrorHandler");

Where ErrorHandler is a class with the following structure:



Even another alternative is to attach a shutdown hook to your runtime. I ran the code below with very low memory settings to make it fail on purpose because of OutOfMemoryError. Look how the showdown hook thread is executed just before exiting the application.

> java -Xms1M -Xmx2M Question1



Be sure to create the shutdown hook thread before your processing, or there could be not enough memory to create the thread.

A final alternative is to manipulate the amount of memory available with the -Xms and -Xmx flags provided by the Java Hotspot Virtual Machines. This way you may avoid the error at all.

I hope this helps!
[ June 01, 2006: Message edited by: Edwin Dalorzo ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you tried try...catch, did you catch Exception? If so, OutOfMemoryError will sail straight through it, because it's not an Exception.

OutOfMemoryError is difficult to handle in Java. Sometimes, the stack trace is unavailable (presumably there wasn't enough memory for it). Also, when trying to handle it, you have to be aware that your handler code must use minimal memory, or it will just provoke another OutOfMemoryError.

Also, do step back and see whether your code really should be running out of memory. Does it really store that much data? If it does, then fine, you just need to give more memory to Java, via -Xmx, as described by another poster. But if you aren't intentionally storing a huge amount of data, perhaps you have a leak. Could objects be building up in some long-lived Collection and never be getting purged, for instance?

Lastly, in some versions of the Sun JVM, at least, you can get OutOfMemoryError in some rare circumstances when you haven't actually run out of heap. Instead, some other JVM internal resource has been exhausted. This is generally considered a JVM bug, but that doesn't mean it won't happen to you!
[ June 02, 2006: Message edited by: Peter Chase ]
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the very helpfull information, i will be spending today exploring your'alls recommendations!

Have a great weekend all!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic