Java Code Examples for java.lang.ref.PhantomReference#isEnqueued()

The following examples show how to use java.lang.ref.PhantomReference#isEnqueued() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Solution.java    From JavaRushTasks with MIT License 5 votes vote down vote up
public void checkListWithReferences(List<PhantomReference<Monkey>> list, String string) {
    int count = 0;
    for (PhantomReference<Monkey> reference : list) {
        if (reference.isEnqueued()) {
            count++;
        }
    }

    System.out.println(String.format("The enqueue reference count is %d (%s GC was called)", count, string));
}
 
Example 2
Source File: TestEnhancer.java    From cglib with Apache License 2.0 5 votes vote down vote up
/**
 * 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());

}