I told you finalizers are bad for you!
Hi all,
Read this interesting thread on forums.sun.com. The quick description is the following. The poster's application kept hitting an OutOfMemory situtation after a few hours of running. A little investigation showed that most of the memory in the heap (850+MB out of a 1.2GB heap) was being held by, you guessed it!, the finalization queue. The reason? A finalizer was getting stuck on I/O (while closing a DB connection) and preventing the rest from running, while more and more objects were added to the queue. Ouch.
The poster advises: Never do I/O in a finalizer! I actually advise: Never use finalizers in the first place!
But, if you really really have to, you should be really really really careful.
Posted at 10:25AM Aug 12, 2008 by tony in Sun | Comments[2]
A google search for "java finalizer" results in this site as the first hit: "http://www.janeg.ca/scjp/gc/finalize.html". I post this comment because they're actually recommending I/O in a finalizer. The fourth bullet item is (ignoring the e.g./i.e. error): "clean-up non-Java resources ie closing a file".
Apparently there's a long way to go before the No I/O lesson is learnt.
Posted by Mark J Musante on August 19, 2008 at 08:12 AM EST #
Fully agree,
Finalizers are evil.
There's almost no good use of them.
One "nice" problem, that I've seen is that people would block in the finalizer, because of using synchronization. Same effect as long running IO :]
Finalizer problems also show up in "mainstream" applications like Netbeans. Check my blog at
http://kohlerm.blogspot.com/2008/06/classical-finalizer-problem-in-netbeans.html.
Posted by Markus Kohler on October 02, 2008 at 03:17 AM EST #