Search in sources :

Example 1 with Reference

use of java.lang.ref.Reference in project ACS by ACS-Community.

the class GarbageCollectionTestHelper method waitForGC.

/**
     * a simple algorithm to wait for GC
     */
public boolean waitForGC(long maxTimeout) {
    long startTime = System.currentTimeMillis();
    long timeOut = maxTimeout;
    Reference retRef = null;
    System.gc();
    while (true) {
        try {
            retRef = refQ.remove(timeOut);
            break;
        } catch (InterruptedException ex) {
            long delta = System.currentTimeMillis() - startTime;
            if (delta < maxTimeout) {
                timeOut = maxTimeout - delta;
                continue;
            }
        }
    // catch
    }
    return (retRef != null);
}
Also used : PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference)

Example 2 with Reference

use of java.lang.ref.Reference in project ACS by ACS-Community.

the class GarbageCollectionTestHelper method complexWaitForGC.

/**
     * a more sophisticated algorithm to wait for Property Change Events
     */
public boolean complexWaitForGC(long maxTimeout) {
    long startTime = System.currentTimeMillis();
    long timeOut = maxTimeout;
    Reference retRef = null;
    int slices = (int) (maxTimeout / GC_SLICE_MILLIS);
    //@
    System.err.println("waiting for " + slices + " slices");
    for (int ix = 0; ix < slices; ix++) {
        System.gc();
        //@
        System.err.println("sleeping for " + GC_SLICE_MILLIS);
        try {
            Thread.currentThread().sleep(GC_SLICE_MILLIS);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        retRef = refQ.poll();
        if (retRef != null) {
            return true;
        }
    }
    return (retRef != null);
}
Also used : PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference)

Example 3 with Reference

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

the class ConstantPoolInfoGC method runTests.

/********** test core **********/
protected void runTests() throws Exception {
    targetClass = startToMain("ConstantPoolGCTarg").location().declaringType();
    if (vm().canGetConstantPool()) {
        byte[] cpbytes = targetClass.constantPool();
        // imitate SoftReference cleared
        Field constantPoolBytesRef = ReferenceTypeImpl.class.getDeclaredField("constantPoolBytesRef");
        constantPoolBytesRef.setAccessible(true);
        Reference softRef = (Reference) constantPoolBytesRef.get(targetClass);
        softRef.clear();
        byte[] cpbytes2 = targetClass.constantPool();
        if (!Arrays.equals(cpbytes, cpbytes2)) {
            failure("Consequent constantPool results vary, first was : " + cpbytes + ", now: " + cpbytes2);
        }
        ;
    } else {
        System.out.println("can get constant pool version not supported");
    }
    /*
         * resume until end
         */
    listenUntilVMDisconnect();
    /*
         * deal with results of test
         * if anything has called failure("foo") testFailed will be true
         */
    if (!testFailed) {
        println("ConstantPoolInfoGC: passed");
    } else {
        throw new Exception("ConstantPoolInfoGC: failed");
    }
}
Also used : Field(java.lang.reflect.Field) Reference(java.lang.ref.Reference)

Example 4 with Reference

use of java.lang.ref.Reference 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 5 with Reference

use of java.lang.ref.Reference 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)

Aggregations

Reference (java.lang.ref.Reference)73 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Map (java.util.Map)13 Field (java.lang.reflect.Field)12 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3