RE: JavaMemoryModel: A problematical case for finalizers

From: Boehm, Hans (
Date: Tue May 20 2003 - 11:54:37 EDT

Josh -

I wasn't referring to your book. We probably disagree about how far you
should go to avoid finalizers. But I certainly agree that they shouldn't
be used if they can be avoided without much pain. And I think that we
should make it possible to use something like the "finalizer guardian"
idiom in your book.

The kinds of things that are likely to cause problems in existing code are:

1) Advice early on to avoid all locks in finalizers. (AFAICT, this was
motivated by early JVM bugs in basically all JVMs.)

2) The tendency to view finalizers as the Java counterpart of C++ destructors.

3) Random misunderstandings like the one I mentioned below.

4) The general absence of warnings about ordering issues. Still few people seem
to understand why runFinalizersOnExit() was completely broken.

5) There's no clear convention about the context in which finalizers can be
explicitly invoked. If System.runFinalization() runs finalizers in the calling
thread, you probably need a convention that it should never be invoked with locks
held. With such a convention you can manage limited resources reasonably reliably
with finalizers; without it, you probably can't.


> -----Original Message-----
> From: Joshua Bloch []
> Sent: Monday, May 19, 2003 11:52 PM
> To: Boehm, Hans
> Cc: 'Martin Trotter';
> Subject: Re: JavaMemoryModel: A problematical case for finalizers
> Hans,
> >2) Convince authors to fix Java textbooks.
> >
> >Some of the finalizer-related discussion in current
> textbooks is so far
> >off track that I would expect a lot of existing code that
> uses finalizers
> >to be in need of repair no matter what we do. (E.g. at
> least one recommends
> >that finalizers be used to clear circular references to make
> life easier
> >for the garbage collector. I haven't seen any textbook discussion
> >of finalizers that I think would be adequate to teach correct usage.
> >
> >
> I have: avoid finalizers. I know that you and I disagree on this one.
> Josh
JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:44 EDT