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);
}
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);
}
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();
}
}
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));
}
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);
}
}
}
Aggregations