Search in sources :

Example 1 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project morphia by mongodb.

the class ReferenceMap method readObject.

/**
     * Reads the contents of this object from the given input stream.
     *
     * @param inp the input stream to read from
     * @throws IOException            if the stream raises it
     * @throws ClassNotFoundException if the stream raises it
     */
private void readObject(final ObjectInputStream inp) throws IOException, ClassNotFoundException {
    inp.defaultReadObject();
    table = new Entry[inp.readInt()];
    threshold = (int) (table.length * loadFactor);
    queue = new ReferenceQueue();
    Object key = inp.readObject();
    while (key != null) {
        final Object value = inp.readObject();
        put(key, value);
        key = inp.readObject();
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue)

Example 2 with ReferenceQueue

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

the class RuntimeThreadInheritanceLeak method main.

public static void main(String[] args) {
    System.err.println("\nRegression test for bug 4404702\n");
    /*
         * HACK: Work around the fact that java.util.logging.LogManager's
         * (singleton) construction also has this bug-- it will register a
         * "shutdown hook", i.e. a thread, which will inherit and pin the
         * current thread's context class loader for the lifetime of the VM--
         * by causing the LogManager to be initialized now, instead of by
         * RMI when our special context class loader is set.
         */
    java.util.logging.LogManager.getLogManager();
    /*
         * HACK: Work around the fact that the non-native, thread-based
         * SecureRandom seed generator (ThreadedSeedGenerator) seems to
         * have this bug too (which had been causing this test to fail
         * when run with jtreg on Windows XP-- see 4910382).
         */
    (new java.security.SecureRandom()).nextInt();
    RuntimeThreadInheritanceLeak obj = new RuntimeThreadInheritanceLeak();
    try {
        ClassLoader loader = URLClassLoader.newInstance(new URL[0]);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference loaderRef = new WeakReference(loader, refQueue);
        System.err.println("created loader: " + loader);
        Thread.currentThread().setContextClassLoader(loader);
        UnicastRemoteObject.exportObject(obj);
        Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
        System.err.println("exported remote object with loader as context class loader");
        loader = null;
        System.err.println("nulled strong reference to loader");
        UnicastRemoteObject.unexportObject(obj, true);
        System.err.println("unexported remote object");
        /*
             * HACK: Work around the fact that the sun.misc.GC daemon thread
             * also has this bug-- it will have inherited our loader as its
             * context class loader-- by giving it a chance to pass away.
             */
        Thread.sleep(2000);
        System.gc();
        System.err.println("waiting to be notified of loader being weakly reachable...");
        Reference dequeued = refQueue.remove(TIMEOUT);
        if (dequeued == null) {
            System.err.println("TEST FAILED: loader not deteced weakly reachable");
            dumpThreads();
            throw new RuntimeException("TEST FAILED: loader not detected weakly reachable");
        }
        System.err.println("TEST PASSED: loader detected weakly reachable");
        dumpThreads();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("TEST FAILED: unexpected exception", e);
    } finally {
        try {
            UnicastRemoteObject.unexportObject(obj, true);
        } catch (RemoteException e) {
        }
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) URLClassLoader(java.net.URLClassLoader) RemoteException(java.rmi.RemoteException) RemoteException(java.rmi.RemoteException)

Example 3 with ReferenceQueue

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

the class JpegWriterLeakTest method main.

public static void main(String[] args) {
    final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();
    final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();
    int count = 2;
    do {
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        final WeakReference<? extends ImageWriter> ref = new WeakReference<>(writer, queue);
        refs.add(ref);
        try {
            final ImageOutputStream os = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
            writer.setOutput(os);
            writer.write(getImage());
        // NB: dispose() or reset() workarounds the problem.
        } catch (IOException e) {
        } finally {
            writer = null;
        }
        count--;
    } while (count > 0);
    System.out.println("Wait for GC...");
    final long testTimeOut = 60000L;
    final long startTime = System.currentTimeMillis();
    while (!refs.isEmpty()) {
        // check for the test timeout
        final long now = System.currentTimeMillis();
        if (now - startTime > testTimeOut) {
            System.out.println();
            throw new RuntimeException("Test FAILED.");
        }
        System.gc();
        try {
            System.out.print(".");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        ;
        Reference<? extends ImageWriter> r = queue.poll();
        if (r != null) {
            System.out.println("Got reference: " + r);
            refs.remove(r);
        }
    }
    System.out.println("Test PASSED.");
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) ImageWriter(javax.imageio.ImageWriter) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WeakReference(java.lang.ref.WeakReference) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 4 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project jackrabbit by apache.

the class SessionGarbageCollectedTest method testSessionsGetGarbageCollected.

public void testSessionsGetGarbageCollected() throws RepositoryException {
    ArrayList<WeakReference<Session>> list = new ArrayList<WeakReference<Session>>();
    ReferenceQueue<Session> detect = new ReferenceQueue<Session>();
    Error error = null;
    try {
        for (int i = 0; ; i++) {
            Session s = getHelper().getReadWriteSession();
            // eat  a lot of memory so it gets garbage collected quickly
            // (or quickly runs out of memory)
            Node n = s.getRootNode().addNode("n" + i);
            n.setProperty("x", new String(new char[1000000]));
            list.add(new WeakReference<Session>(s, detect));
            if (detect.poll() != null) {
                break;
            }
        }
    } catch (OutOfMemoryError e) {
        error = e;
    }
    for (int i = 0; i < list.size(); i++) {
        Reference<Session> ref = list.get(i);
        Session s = ref.get();
        if (s != null) {
            s.logout();
        }
    }
    if (error != null) {
        throw error;
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) WeakReference(java.lang.ref.WeakReference) Session(javax.jcr.Session)

Example 5 with ReferenceQueue

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

the class ReferenceTest method test_isEnqueued.

/**
     * java.lang.ref.Reference#isEnqueued()
     */
public void test_isEnqueued() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Should start off not enqueued.", !ref.isEnqueued());
    ref.enqueue();
    assertTrue("Should now be enqueued.", ref.isEnqueued());
    ref.enqueue();
    assertTrue("Should still be enqueued.", ref.isEnqueued());
    rq.poll();
    // This fails ...
    assertTrue("Should now be not enqueued.", !ref.isEnqueued());
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Aggregations

ReferenceQueue (java.lang.ref.ReferenceQueue)39 WeakReference (java.lang.ref.WeakReference)20 PhantomReference (java.lang.ref.PhantomReference)14 SoftReference (java.lang.ref.SoftReference)14 Reference (java.lang.ref.Reference)13 Test (org.testng.annotations.Test)8 URLClassLoader (java.net.URLClassLoader)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 SideEffect (dalvik.annotation.SideEffect)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 SocketPermission (java.net.SocketPermission)1 MarshalledObject (java.rmi.MarshalledObject)1 Remote (java.rmi.Remote)1 RemoteException (java.rmi.RemoteException)1