Handling OutOfMemoryError Gracefully

Hi,

I am looking for solution to handle OutOfMemoryError or any other JVM Error gracefully. I want to send emails to system-admin and then terminate my JVM.

I am not able to find any good resource on using log4j for handling errors.

If I code 搕hrow new Error(搄vm error occurred, shutting down system?;

My application does not get this information, it keeps behaving normally as though it is trying to process something. I don抰 see my message getting printed.

I can make this work using catch(Error e) {...}, but I think catching errors in code is not a good idea. I would like to know if log4j has some better solution.

Can someone please direct my in right direction?

[713 byte] By [Roxiea] at [2007-10-2 23:09:48]
# 1

Hi Roxie,

I had the need to do this in a program a few years ago. I looked into it pretty extensively and was not successful. Although I didn't look it up to quote it, I finally found in the Java spec (1.3 or 1.4 probably) that the OutOfMemoryError is very difficult, if not impossible to catch. And if you think about it, it makes sense. If you really are out of memory, there probably isn't any way to load in any more byte code to run the error handler (or something to that affect). You will probably find more success in trying to determine the maximum amount of memory you need and adjusting the JVM settings accordingly.

nickolausa at 2007-7-14 6:23:46 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Thanks nick. I cannot adjust jvm parameters, i have allocated sufficient memory. This error happens only when iam running huge load on my server, this is very rare condition.I am looking in to log4j to check the error handling.
Roxiea at 2007-7-14 6:23:46 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...