Why is finalize wrong?

Andrew Haley aph at redhat.com
Wed Sep 3 09:58:55 UTC 2014


On 03/09/14 01:15, Stanimir Simeonoff wrote:

> Like David Lloyd mentioned finalize() can be invoked concurrently to
> some methods (if there is no reachability to 'this'). OTOH I see
> finalize useful for managing native resources mostly and possibly
> stopping (signaling) threads left unmanaged. In that particular case
> synchronization is a must, so it comes naturally.

But finalize isn't useful for managing native resources because
finalizers may or may not run, as you note.  People will test their
programs with one implementation then discover, when their programs
are deployed, that they sometimes mysteriously fail.  In particular,
you might run out of file handles.

As Jarolsav put it, "I don't think this is explainable to regular Java
developers."  As David Lloyd put it, "If you still think that finalize
is a good idea, given that it's basically defective *and* there is
almost always a better solution, then I will be quite astounded."  As
I put it, "finalize is broken."

Andrew.



More information about the core-libs-dev mailing list