Log outofmemory

Hai,Is it possible to log outofmemory exception. if possible please guide me. Thanks in advanceRegards Prasad
[137 byte] By [prasadmcaa] at [2007-11-27 5:46:29]
# 1

I'm not sure exactly what you mean by "log OutOfMemoryError" but, generally speaking when you're out of memory it's a bit difficult to do much, since almost everything (including formatting strings for printing) allocates objects in the java heap. So the first thing you have to do is free up some memory.

One approach is to allocate a buffer when your program starts and keep a reference to it so it is not recycled by the garbage collector. Then you can catch OutOfMemoryError and in the try block, set the reference to null so the buffer can be recycled, then try to log your data.

char buffer = new char[1024*1024];

try {

...

} catch (OutOfMemoryError oome) {

buffer = null; // free some space

... your logging goes here ...

}

Unfortunately, this is not bullet-proof. Other threads will also be attempting to allocate and there's no way to ensure that the thread that freed the buffer gets to use the space.

If all your threads run similar code, you can try to add some synchronization in the catch block.That will improve your odds of successfully logging the error, but won't guarantee it.

jxca at 2007-7-12 15:29:24 > top of Java-index,Desktop,Runtime Environment...