Re: JavaMemoryModel: Implementing KeepAlive

From: Bill Pugh (pugh@cs.umd.edu)
Date: Wed Apr 21 2004 - 22:40:41 EDT


On Apr 21, 2004, at 8:30 PM, Jerry Schwarz wrote:

> At 12:02 PM 4/21/2004, Bill Pugh wrote:
>
> OK, here is a class that implements keepAlive:
>
> public class FinalizerHelp {
>   static volatile Object v;
>   public static void keepAlive(Object o) {
>     v = o;
>     v = null;
>     }
>   public static void getReadyToFinalize() {
>     Object o = v;
>     }
> }
>
>
> Take Hans'es example and add a call to keepAlive.
> for (i = 0; i < 10; ++i) {
>   sum += x.a[i];
> }
> FinalizerHelp.keepAlive(a)
> ===
>
> I don't think this has the desired effect. The compiler first moves
> the reference to x outside the loop and inlines keepAlive to
>
>     v = o;
>     v = null;
>
> Then it eliminates v=o.

Nope. This isn't allowed.

The semantics we introduced to make finalizer guardians work ensures
that
if a reachable variable contains a reference to an object, that object
is also reachable,
even if that field is never read.

Now it occurs to me that perhaps this is too strong a requirement, and
it will prevent some useful optimizations.
I don't see a problem with keeping compilers from eliminating stores to
volatile fields, but eliminating overwritten
stores to non-volatile fields is something we might want to allow.

Bill

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



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