| Author |
Exception in thread "main" java.lang.OutOfMemoryError
|
bob connolly
Ranch Hand
Joined: Mar 10, 2004
Posts: 204
|
|
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:
|
 |
Edwin Dalorzo
Ranch Hand
Joined: Dec 31, 2004
Posts: 961
|
|
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 ]
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
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 ]
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
bob connolly
Ranch Hand
Joined: Mar 10, 2004
Posts: 204
|
|
Thanks all for the very helpfull information, i will be spending today exploring your'alls recommendations! Have a great weekend all!
|
 |
 |
|
|
subject: Exception in thread "main" java.lang.OutOfMemoryError
|
|
|