Search in sources :

Example 1 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class PhantomReferenceTest method test_gcInteraction.

/**
     * java.lang.Runtime#gc()
     */
public void test_gcInteraction() {
    class TestPhantomReference<T> extends PhantomReference<T> {

        public TestPhantomReference(T referent, ReferenceQueue<? super T> q) {
            super(referent, q);
        }

        public boolean enqueue() {
            // Initiate another GC from inside enqueue() to
            // see if it causes any problems inside the VM.
            Runtime.getRuntime().gc();
            return super.enqueue();
        }
    }
    final ReferenceQueue rq = new ReferenceQueue();
    final PhantomReference[] tprs = new PhantomReference[4];
    class TestThread extends Thread {

        public void run() {
            // Create the object in a separate thread to ensure
            // it will be gc'ed.
            Object obj = new Object();
            tprs[0] = new TestPhantomReference(obj, rq);
            tprs[1] = new TestPhantomReference(obj, rq);
            tprs[2] = new TestPhantomReference(obj, rq);
            tprs[3] = new TestPhantomReference(obj, rq);
        }
    }
    try {
        Thread t = new TestThread();
        t.start();
        t.join();
        FinalizationTester.induceFinalization();
        assertNull("get() should return null.", tprs[0].get());
        assertNull("get() should return null.", tprs[1].get());
        assertNull("get() should return null.", tprs[2].get());
        assertNull("get() should return null.", tprs[3].get());
        for (int i = 0; i < 4; i++) {
            Reference r = rq.remove(100L);
            assertNotNull("Reference should have been enqueued.", r);
        }
        // These are to make sure that tprs and its elements don't get
        // optimized out.
        assertNull("get() should return null.", tprs[0].get());
        assertNull("get() should return null.", tprs[1].get());
        assertNull("get() should return null.", tprs[2].get());
        assertNull("get() should return null.", tprs[3].get());
    } catch (InterruptedException e) {
        fail("InterruptedException : " + e.getMessage());
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) PhantomReference(java.lang.ref.PhantomReference)

Example 2 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class ReferenceQueueTest method test_poll.

/**
     * java.lang.ref.ReferenceQueue#poll()
     */
public void test_poll() {
    // store in a static so it won't be gc'ed because the jit
    // optimized it out
    b = new Boolean(true);
    Object obj = new Object();
    String str = "Test";
    SoftReference sr = new SoftReference(b, rq);
    WeakReference wr = new WeakReference(obj, rq);
    PhantomReference pr = new PhantomReference(str, rq);
    assertNull(rq.poll());
    sr.enqueue();
    wr.enqueue();
    pr.enqueue();
    try {
        assertNull("Remove failed.", rq.poll().get());
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    try {
        assertEquals("Remove failed.", obj, (rq.poll().get()));
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    try {
        assertTrue("Remove failed.", ((Boolean) rq.poll().get()).booleanValue());
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    assertNull(rq.poll());
    sr.enqueue();
    wr.enqueue();
    FinalizationTester.induceFinalization();
    assertNull(rq.poll());
}
Also used : SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 3 with PhantomReference

use of java.lang.ref.PhantomReference in project jdk8u_jdk by JetBrains.

the class Disposer method add.

/**
     * Performs the actual registration of the target object to be disposed.
     * @param target Object to be registered, or if target is an instance
     *               of DisposerTarget, its associated disposer referent
     *               will be the Object that is registered
     * @param rec the associated DisposerRecord object
     * @see DisposerRecord
     */
synchronized void add(Object target, DisposerRecord rec) {
    if (target instanceof DisposerTarget) {
        target = ((DisposerTarget) target).getDisposerReferent();
    }
    java.lang.ref.Reference ref;
    if (refType == PHANTOM) {
        ref = new PhantomReference(target, queue);
    } else {
        ref = new WeakReference(target, queue);
    }
    records.put(ref, rec);
}
Also used : Reference(java.lang.ref.Reference) PhantomReference(java.lang.ref.PhantomReference) WeakReference(java.lang.ref.WeakReference)

Example 4 with PhantomReference

use of java.lang.ref.PhantomReference in project cglib by cglib.

the class TestEnhancer method testSourceCleanAfterClassLoaderDispose.

/**
     * Verifies that the cache in {@link AbstractClassGenerator} SOURCE doesn't
     * leak class definitions of classloaders that are no longer used.
     */
public void testSourceCleanAfterClassLoaderDispose() throws Throwable {
    ClassLoader custom = new ClassLoader(this.getClass().getClassLoader()) {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (EA.class.getName().equals(name)) {
                InputStream classStream = this.getClass().getResourceAsStream("/net/sf/cglib/proxy/EA.class");
                byte[] classBytes;
                try {
                    classBytes = toByteArray(classStream);
                    return this.defineClass(null, classBytes, 0, classBytes.length);
                } catch (IOException e) {
                    return super.loadClass(name);
                }
            } else {
                return super.loadClass(name);
            }
        }
    };
    PhantomReference<ClassLoader> clRef = new PhantomReference<ClassLoader>(custom, new ReferenceQueue<ClassLoader>());
    buildAdvised(custom);
    custom = null;
    for (int i = 0; i < 10; ++i) {
        System.gc();
        Thread.sleep(100);
        if (clRef.isEnqueued()) {
            break;
        }
    }
    assertTrue("CGLIB should allow classloaders to be evicted. PhantomReference<ClassLoader> was not cleared after 10 gc cycles," + "thus it is likely some cache is preventing the class loader to be garbage collected", clRef.isEnqueued());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PhantomReference(java.lang.ref.PhantomReference) IOException(java.io.IOException)

Example 5 with PhantomReference

use of java.lang.ref.PhantomReference in project robovm by robovm.

the class PhantomReferenceTest method test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue.

/**
     * java.lang.ref.PhantomReference#PhantomReference(java.lang.Object,
     *        java.lang.ref.ReferenceQueue)
     */
public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(true);
    try {
        PhantomReference pr = new PhantomReference(bool, rq);
        // Allow the finalizer to run to potentially enqueue
        Thread.sleep(1000);
        assertTrue("Initialization failed.", !pr.isEnqueued());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    // need a reference to bool so the jit does not optimize it away
    assertTrue("should always pass", bool.booleanValue());
    boolean exception = false;
    try {
        new PhantomReference(bool, null);
    } catch (NullPointerException e) {
        exception = true;
    }
    assertTrue("Should not throw NullPointerException", !exception);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference)

Aggregations

PhantomReference (java.lang.ref.PhantomReference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)8 WeakReference (java.lang.ref.WeakReference)5 Reference (java.lang.ref.Reference)4 SoftReference (java.lang.ref.SoftReference)4 Test (org.junit.Test)2 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MethodHandle (java.lang.invoke.MethodHandle)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1