On 03/07/2013 11:09 PM, Thomas Neidhart wrote:
> Hi,
> 
> I have installed the following JDK on my machine:
> 
> java version "1.6.0"
> Java(TM) SE Runtime Environment (build pxi3260sr12-20121025_01(SR12))
> IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux x86-32
> jvmxi3260sr12-20121024_126067 (JIT enabled, AOT enabled)
> J9VM - 20121024_126067
> JIT  - r9_20120914_26057
> GC   - 20120928_AA)
> JCL  - 20121014_01
> 
> When I try to run the following test from the IBM developerworks wrt
> WeakReferences and ReferenceQueue, the reference is never put on the
> reference queue (the problem that I also had in the WeakHashtable testcase):
> 
> url: http://www.ibm.com/developerworks/library/j-refs/
> 
> output:
> 
> Example of incorrectly holding a strong reference
> object is MyObject@3b203b2
> The weak reference is java.lang.ref.WeakReference@46d046d
> Polling the reference queue returns null
> Getting the referent from the weak reference returns MyObject@3b203b2
> Calling GC
> Polling the reference queue returns null
> Getting the referent from the weak reference returns null
> 
> Example of correctly releasing a strong reference
> object is MyObject@2bd62bd6
> The weak reference is java.lang.ref.WeakReference@37c637c6
> Polling the reference queue returns null
> Getting the referent from the weak reference returns MyObject@2bd62bd6
> Set the obj reference to null and call GC
> Polling the reference queue returns null
> Getting the referent from the weak reference returns null
> In finalize method for this object: MyObject@3b203b2
> In finalize method for this object: MyObject@2bd62bd6
> 
> 
> Polling the reference queue always returns null, even when the
> WeakReference has lost its referent.
> 
> Can somebody confirm this?

Another weird thing:

Consider this testcase:

    public static void main (String[] args) {
        Object obj = new Object();
        ReferenceQueue queue = new ReferenceQueue();
        Reference ref = new WeakReference(obj, queue);

        obj = null;
        System.gc();

// first approach

        Reference ref1 = null;
        while ( (ref1 = queue.poll()) == null ) {
            System.out.println(ref1);
        }
        System.out.println(ref1);

// second approach
//        Reference ref1 = null;
//        do {
//            ref1 = queue.poll();
//            System.out.println("Polling the reference queue returns " +
//                           ref);
//        } while (ref == null);
    }

The first one results in an endless loop of null outputs
The second one returns immediately an reference

This with IBM JDK 6. With OpenJDK 6 I get the same result with both
approaches.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to