Search in sources :

Example 11 with Reference

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

the class DGCImplInsulation method main.

public static void main(String[] args) throws Exception {
    TestLibrary.suggestSecurityManager(null);
    Permissions perms = new Permissions();
    perms.add(new SocketPermission("*:1024-", "listen"));
    AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(new CodeSource(null, (Certificate[]) null), perms) });
    Remote impl = new DGCImplInsulation();
    ;
    try {
        Remote stub = (Remote) java.security.AccessController.doPrivileged(new ExportAction(impl));
        System.err.println("exported remote object; local stub: " + stub);
        MarshalledObject mobj = new MarshalledObject(stub);
        stub = (Remote) mobj.get();
        System.err.println("marshalled/unmarshalled stub: " + stub);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference weakRef = new WeakReference(impl, refQueue);
        impl = null;
        System.gc();
        if (refQueue.remove(TIMEOUT) == weakRef) {
            throw new RuntimeException("TEST FAILED: remote object garbage collected");
        } else {
            System.err.println("TEST PASSED");
            stub = null;
            System.gc();
            Thread.sleep(2000);
            System.gc();
        }
    } finally {
        try {
            UnicastRemoteObject.unexportObject(impl, true);
        } catch (Exception e) {
        }
    }
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) SocketPermission(java.net.SocketPermission) Remote(java.rmi.Remote) CodeSource(java.security.CodeSource) AccessControlContext(java.security.AccessControlContext) MarshalledObject(java.rmi.MarshalledObject) WeakReference(java.lang.ref.WeakReference) Permissions(java.security.Permissions) Certificate(java.security.cert.Certificate)

Example 12 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 13 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 14 with Reference

use of java.lang.ref.Reference in project ignite by apache.

the class CacheWeakQueryIteratorsHolder method checkWeakQueue.

/**
     * Closes unreachable iterators.
     */
public void checkWeakQueue() {
    for (Reference itRef = refQueue.poll(); itRef != null; itRef = refQueue.poll()) {
        try {
            WeakReference weakRef = (WeakReference) itRef;
            AutoCloseable rsrc = refs.remove(weakRef);
            if (rsrc != null)
                rsrc.close();
        } catch (Exception e) {
            U.error(log, "Failed to close iterator.", e);
        }
    }
}
Also used : Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) NoSuchElementException(java.util.NoSuchElementException)

Example 15 with Reference

use of java.lang.ref.Reference in project intellij-community by JetBrains.

the class DebugReflectionUtil method queueStronglyReferencedValues.

private static void queueStronglyReferencedValues(Queue<BackLink> queue, @NotNull Object root, @NotNull Condition<Object> shouldExamineValue, @NotNull BackLink backLink) {
    Class rootClass = root.getClass();
    for (Field field : getAllFields(rootClass)) {
        String fieldName = field.getName();
        // do not follow weak/soft refs
        if (root instanceof Reference && "referent".equals(fieldName))
            continue;
        Object value;
        try {
            value = field.get(root);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        queue(value, field, backLink, queue, shouldExamineValue);
    }
    if (rootClass.isArray()) {
        try {
            //noinspection ConstantConditions
            for (Object value : (Object[]) root) {
                queue(value, null, backLink, queue, shouldExamineValue);
            }
        } catch (ClassCastException ignored) {
        }
    }
    // check for objects leaking via static fields. process initialized classes only
    if (root instanceof Class && isInitialized((Class) root)) {
        for (Field field : getAllFields((Class) root)) {
            if ((field.getModifiers() & Modifier.STATIC) == 0)
                continue;
            try {
                Object value = field.get(null);
                queue(value, field, backLink, queue, shouldExamineValue);
            } catch (IllegalAccessException ignored) {
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Reference(java.lang.ref.Reference)

Aggregations

Reference (java.lang.ref.Reference)31 WeakReference (java.lang.ref.WeakReference)17 PhantomReference (java.lang.ref.PhantomReference)10 ReferenceQueue (java.lang.ref.ReferenceQueue)10 SoftReference (java.lang.ref.SoftReference)10 Field (java.lang.reflect.Field)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 IOException (java.io.IOException)2 AccessControlContext (java.security.AccessControlContext)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Supplier (com.google.common.base.Supplier)1 SoftReference (com.intellij.reference.SoftReference)1 SideEffect (dalvik.annotation.SideEffect)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1