On 3/14/07, Salikh Zakirov <[EMAIL PROTECTED]> wrote:

Elena Semukhina wrote:
> The 'ref' object in the test is a PhantomReference object. The spec for
> PhantomReference reads:
> "Unlike soft and weak references, phantom references are not
automatically
> cleared by the garbage collector as they are enqueued."

PhantomReference.get() always returns null.
The line that fails the test is different one:

     enqueued = queue.poll();
     if (enqueued == null) {
         System.out.println("FAIL: reference was not enqueued");
         return;
     }

> I think they should not be automatically cleared even because all its
> methods are inlined since we must ensure that their get() method always
> returns null after the referents of phantom references are reclaimed.

the problem in this particular test is in the fact, that OPT was able to
inline
all uses of local variable 'ref' and eliminate them, thus making the
PhantomReference object itself unreachable.

Reading the javadoc of weak references (java.lang.ref package
description),

> The relationship between a registered reference object and its queue is
> one-sided. That is, a queue does not keep track of the references that
are
> registered with it. If a registered reference becomes unreachable
itself,
> then it will never be enqueued. It is the responsibility of the program
> using reference objects to ensure that the objects remain reachable for
as
> long as the program is interested in their referents.

we see the exact reason why the reference object is never enqueued.

The simplest way to fix the test is to declare PhantomReference variable
as static:

       static Reference ref;

       public static void main(String[] args) {

               // ...

               ref = new PhantomReference(referent, queue);


Salikh,
thank you for clear explanation and the suggested fix. I created a patch and
attached it to HARMONY-3379.

Thanks,
Elena

Reply via email to