JavaMemoryModel: Implementing KeepAlive

From: Bill Pugh (
Date: Wed Apr 21 2004 - 15:02:18 EDT

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;

The semantics we provided to ensure that finalizer guardians would work
will also ensure that an object is still reachable if we are going to
write a reference to that object into a global variable.

Because the write is a volatile write, preceding memory accesses can't
be reordered with the call to keepAlive.

The one point of ugliness here is that each finalizer should call


JavaMemoryModel mailing list -

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