Example usage for com.google.common.testing GcFinalization awaitClear

List of usage examples for com.google.common.testing GcFinalization awaitClear

Introduction

In this page you can find the example usage for com.google.common.testing GcFinalization awaitClear.

Prototype

public static void awaitClear(final WeakReference<?> ref) 

Source Link

Document

Waits until the given weak reference is cleared, invoking the garbage collector as necessary to try to ensure that this will happen.

Usage

From source file:com.google.inject.internal.WeakKeySetUtils.java

public static void awaitClear(WeakReference<?> ref) {
    // GcFinalization *should* do it, but doesn't work well in practice...
    // so we put a second latch and wait for a ReferenceQueue to tell us.
    Object data = ref.get();//  w ww.j  a v a  2  s.c om
    ReferenceQueue<Object> queue = null;
    WeakReference extraRef = null;
    if (data != null) {
        queue = new ReferenceQueue<Object>();
        extraRef = new WeakReference<Object>(data, queue);
        data = null;
    }
    GcFinalization.awaitClear(ref);
    if (queue != null) {
        try {
            assertSame("queue didn't return ref in time", extraRef, queue.remove(5000));
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}