[concurrency-interest] Fences.keepAlive

Jeremy Manson jeremy.manson at gmail.com
Sun Jan 18 20:08:27 EST 2009


Oh, right, we always meant to do that.  Good idea!

I have two concerns with the javadoc:

First, I think it might be unclear to the reader that the problem
arises in that example, because nothing is seen to call the
constructor.  Also, we specified it so that you couldn't finalize the
object until the constructor until the constructor completes, so, for
the purposes of making the point more clearly, it might be a little
odd to call keepAlive on an object before its constructor completes.

Second, I think it might be helpful to have a stronger statement of
the problem in the Javadoc.  When I explain that this can happen to
people, they are usually mystified and then shocked.  Something like
"When a runtime can detect that no reference to an object will ever be
stored in the heap, the garbage collector can reclaim that object at
any time.  This can happen even if the object itself, of fields of
that object, are still in use.  See Section 12.6.1 of the Java
Language Specification for more details."  might be more complete.

Jeremy


On Wed, Jan 14, 2009 at 6:20 AM, Doug Lea <dl at cs.oswego.edu> wrote:
>
> [Same cross-post conventions as the last one.]
>
> As another make-good for a long-standing problem, the
> Fences class should include what amounts to a fence with
> respect to the garbage collector. This addresses a problem that
> Hans Boehm has written and talked about for years.
> (Google finds his JavaOne 2005 slides at
> http://gceclub.sun.com.cn/java_one_online/2005/TS-3281/ts-3281.pdf)
>
> This turns out not to be expressible using other kinds of
> fences. It tells the compiler/runtime that the ref in
> question must be considered as being used even if nothing in
> the program itself indicates it. (The suggested JVM implementation
> of this is an "opaque fence" that bypasses/evades any
> mechanics or optimizations that might cause ref to vanish.)
>
> Here's a first pass at javadoc/spec. Comments welcome.
>
>    /**
>     * Ensures that object referenced by the given reference is not
>     * reclaimable by garbage collection until after the invocation of
>     * this method. Among other related usages, this method may assist
>     * efforts to ensure that finalization associated with garbage
>     * collection does not occur before the finalized resources have
>     * even been established.  For example:
>     *
>     * <pre>
>     * class Resource {
>     *   static NativeResource nativeResourceArray = ...
>     *   static void registerInArray(int index) { ... }
>     *   int myIndex;
>     *   void setup() {
>     *      int i = myIndex;
>     *      registerInArray(i);
>     *      keepAlive(this);
>     *   }
>     *   Resource(...) {
>     *     myIndex = ...
>     *     setup();
>     *   }
>     * }
>     * </pre>
>     * @param ref the reference
>     */
>    public static <T> keepAlive(T ref);
>
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>


More information about the Concurrency-interest mailing list