Search in sources :

Example 1 with WeakReference

use of java.lang.ref.WeakReference in project groovy by apache.

the class ClassInfoLeakStressTest method testLeak.

@Test
public void testLeak() {
    assertFalse(Boolean.getBoolean("groovy.use.classvalue"));
    for (int i = 0; i < NUM_OBJECTS; i++) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        Class scriptClass = gcl.parseClass("int myvar = " + i);
        ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
        Reference<ClassLoader> classLoaderRef = new WeakReference<ClassLoader>(gcl, classLoaderQueue);
        Reference<Class<?>> classRef = new WeakReference<Class<?>>(scriptClass, classQueue);
        Reference<ClassInfo> classInfoRef = new WeakReference<ClassInfo>(ci, classInfoQueue);
        refList.add(classLoaderRef);
        refList.add(classRef);
        refList.add(classInfoRef);
        gcl = null;
        scriptClass = null;
        ci = null;
        GCUtils.gc();
    }
    // Add new class to help evict the last collected entry
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class scriptClass = gcl.parseClass("int myvar = 7777");
    ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
    GCUtils.gc();
    // All objects should have been collected
    assertEquals("GroovyClassLoaders not collected by GC", NUM_OBJECTS, queueSize(classLoaderQueue));
    assertEquals("Script Classes not collected by GC", NUM_OBJECTS, queueSize(classQueue));
    int ciSize = queueSize(classInfoQueue);
    assertEquals("ClassInfo objects [" + ciSize + "] collected by GC, expected [" + NUM_OBJECTS + "]", NUM_OBJECTS, ciSize);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) WeakReference(java.lang.ref.WeakReference) GroovyClassLoader(groovy.lang.GroovyClassLoader) Test(org.junit.Test)

Example 2 with WeakReference

use of java.lang.ref.WeakReference in project cglib by cglib.

the class Enhancer method wrapCachedClass.

@Override
protected Object wrapCachedClass(Class klass) {
    Class[] argumentTypes = this.argumentTypes;
    if (argumentTypes == null) {
        argumentTypes = Constants.EMPTY_CLASS_ARRAY;
    }
    EnhancerFactoryData factoryData = new EnhancerFactoryData(klass, argumentTypes, classOnly);
    Field factoryDataField = null;
    try {
        // The subsequent dance is performed just once for each class,
        // so it does not matter much how fast it goes
        factoryDataField = klass.getField(FACTORY_DATA_FIELD);
        factoryDataField.set(null, factoryData);
        Field callbackFilterField = klass.getDeclaredField(CALLBACK_FILTER_FIELD);
        callbackFilterField.setAccessible(true);
        callbackFilterField.set(null, this.filter);
    } catch (NoSuchFieldException e) {
        throw new CodeGenerationException(e);
    } catch (IllegalAccessException e) {
        throw new CodeGenerationException(e);
    }
    return new WeakReference<EnhancerFactoryData>(factoryData);
}
Also used : Field(java.lang.reflect.Field) WeakReference(java.lang.ref.WeakReference)

Example 3 with WeakReference

use of java.lang.ref.WeakReference in project Meizhi by drakeet.

the class SmoothAppBarLayout method removeOnOffsetSyncedListener.

public void removeOnOffsetSyncedListener(OnOffsetSyncedListener listener) {
    Iterator i = mOffsetSyncedListeners.iterator();
    while (true) {
        OnOffsetSyncedListener item;
        do {
            if (!i.hasNext()) {
                return;
            }
            WeakReference ref = (WeakReference) i.next();
            item = (OnOffsetSyncedListener) ref.get();
        } while (item != listener && item != null);
        i.remove();
    }
}
Also used : WeakReference(java.lang.ref.WeakReference) Iterator(java.util.Iterator)

Example 4 with WeakReference

use of java.lang.ref.WeakReference in project Meizhi by drakeet.

the class SmoothAppBarLayout method addOnOffsetSyncedListener.

public void addOnOffsetSyncedListener(OnOffsetSyncedListener listener) {
    int i = 0;
    for (int z = this.mOffsetSyncedListeners.size(); i < z; ++i) {
        WeakReference ref = (WeakReference) this.mOffsetSyncedListeners.get(i);
        if (ref != null && ref.get() == listener) {
            return;
        }
    }
    this.mOffsetSyncedListeners.add(new WeakReference(listener));
}
Also used : WeakReference(java.lang.ref.WeakReference)

Example 5 with WeakReference

use of java.lang.ref.WeakReference in project Meizhi by drakeet.

the class SmoothAppBarLayout method syncOffset.

public void syncOffset() {
    int i = 0;
    for (int z = mOffsetSyncedListeners.size(); i < z; ++i) {
        WeakReference ref = (WeakReference) mOffsetSyncedListeners.get(i);
        OnOffsetSyncedListener listener = ref != null ? (OnOffsetSyncedListener) ref.get() : null;
        if (listener != null) {
            listener.onOffsetSynced(this);
        }
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Aggregations

WeakReference (java.lang.ref.WeakReference)277 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Field (java.lang.reflect.Field)18 Activity (android.app.Activity)17 Method (java.lang.reflect.Method)15 File (java.io.File)14 IOException (java.io.IOException)13 URLClassLoader (java.net.URLClassLoader)13 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Iterator (java.util.Iterator)12 Resources (android.content.res.Resources)10 Handler (android.os.Handler)10 AbstractModule (com.google.inject.AbstractModule)10 Injector (com.google.inject.Injector)10 ArrayMap (android.util.ArrayMap)9 URL (java.net.URL)8