RE: JavaMemoryModel: Finalization idioms

From: Evan Ireland (
Date: Sun May 01 2005 - 22:58:11 EDT


Please excuse my ignorance, but I can't understand why you believe
it would be valid in the example code you gave below for the finalizer
for 'man' to run before man.useA() or man.useB() have been called.

The javadocs for java.lang.Object.finalize state:

  The general contract of finalize is that it is invoked if and
  when the JavaTM virtual machine has determined that there is
  no longer any means by which this object can be accessed by
  any thread that has not yet died, except as a result of an
  action taken by the finalization of some other object or class
  which is ready to be finalized.

It seems to me that if man.finalize() were to be called before
man.useA() or man.useB() has completed, the documented contract
of finalize has not been met.

(Surely a call to man.useA() or man.useB() constitutes access
to the object 'man' by a thread that has not yet died).

> -----Original Message-----
> From:
> []On Behalf Of David Holmes
> Sent: Sunday, 1 May 2005 9:58 p.m.
> To: jmm
> Subject: RE: JavaMemoryModel: Finalization idioms
> Hans,
> > I believe it works either way. You can also use a
> > synchronized(this){} at the end
> > of every ordinary method call which might be the last call on the
> > object. And it hides this mess inside the object with
> > the finalizer.
> I don't see how this is the case. If the object's own methods perform the
> sync block then that only ensures that the object remains reachable during
> execution of that method. If the client uses the object via a
> local then the
> client must ensure there is no premature finalization.
> eg.
> void someMethod() {
> ResourceManager man = new ResourceManager();
> int result1 = man.useA();
> int result2 = man.useB();
> }
> If the ResourceManager finalizer clears resources A and B then
> the caller of
> someMethod needs to prevent premature finalization of the ResourceManager
> instance. If useA and useB include a sync block at the end that
> only ensures
> that the finalizer will run either before useA, between useA and useB, or
> after useB.
> My understanding of this issue is that the client code - that which is
> referencing the object - must do something to ensure the object remains
> reachable, but following some protocol involving the finalize
> method of the
> object concerned.
> Cheers,
> David Holmes
> -------------------------------
> JavaMemoryModel mailing list -

JavaMemoryModel mailing list -

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