RE: JavaMemoryModel: Finalizer question

From: Boehm, Hans (
Date: Mon Apr 19 2004 - 19:11:23 EDT

I think this is not guaranteed for two distinct reasons:

1. The current spec does not guarantee that the earlier write is
no longer visible. If I remember correctly, this was somewhat intentional.
The obvious meaningful solution would be to require that both writes happen-before
the finalizer. But that would be expensive, since it would inhibit
any reordering that might involve a write to a finalizable object.

I was concerned earlier that the guarantee involving writes to
fields of finalizable objects is useful only if the field is volatile.
In that case, we can just require that it happens-before the finalizer.
That may still be the right answer.

We could also "fix" this "problem" by requiring only the last write
of a given field from a given thread to be visible in the finalizer.
I wouldn't object to that either, but I'm still not sure it's important
for real code, since that usually also depends on ordering of writes
outside the (usually nearly dead and hence mostly uninteresting) object.

2. I think you assume effectively that the r4=r3.x assignment can't
happen early, before the loop. I think the only thing really preventing that is that
in the absence of finalization it occurs after a potentially
nonterminating computation.

I think the current model does restrict moving reads before potentially
nonterminating loops. Am I right that it does? Is that what we want?

It's hard to prove that

for (p = list; p != null; p = len++;

terminates. (It doesn't if p points to a cyclic list.)
But it seems to me that you would like to be able to
move loads past it. It's easy to prove that it only changes the
local variables len and p.

Or did I miss something else?


> -----Original Message-----
> From:
> []On Behalf Of Bill Pugh
> Sent: Monday, April 19, 2004 12:07 PM
> To:
> Subject: JavaMemoryModel: Finalizer question
> OK, here is a quick finalizer question.
> class A {
> static A g;
> long x;
> protected void finalize() {
> g = this;
> long r1 = x;
> // ...
> }
> }
> Thread 1:
> A r2 = new A();
> r2.x = 1;
> r2.x = -1;
> Thread 2:
> do {
> r3 = A.g;
> } while (r3 == null);
> r4 = r3.x;
> Are we guaranteed that r1 and r4 will both see the value -1?
> At a intuitive level, before running the finalizer on an object o,
> we need to make sure that if two conflicting writes w1 and w2
> have been performed to a field/element of o, and w1 happens-before
> w2, then we need to make sure that before the finalizer is run, the
> write w1 becomes no longer accessible.
> ---
> Bill
> -------------------------------
> JavaMemoryModel mailing list -
JavaMemoryModel mailing list -

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