Details
-
Bug
-
Resolution: Done
-
Major - P3
-
None
-
2.11.3
-
None
Description
ClassAncestry stores references to my classes (like enums that I use in the queries), never releases these references, which prevent the WebappClassloader and all the classes it loaded to be reclaimed by the garbage collector when I undeploy my webapp.
I'm currently working around using reflection.
@SuppressWarnings("unchecked")
|
public static void cleanupMongoDriver(ClassLoader webappClassLoader) {
|
|
|
ClassLoader bsonLoader = ClassMap.class.getClassLoader();
|
Class<?> clazz;
|
try {
|
clazz = Class.forName("org.bson.util.ClassAncestry", true, bsonLoader);
|
} catch (ClassNotFoundException e) {
|
return;
|
}
|
|
|
ConcurrentMap<Class<?>, List<Class<?>>> _ancestryCache;
|
try {
|
Field f = clazz.getDeclaredField("_ancestryCache");
|
f.setAccessible(true);
|
_ancestryCache = (ConcurrentMap<Class<?>, List<Class<?>>>) f.get(null);
|
} catch (Exception e) {
|
return;
|
}
|
List<Class<?>> toRemove = new ArrayList<Class<?>>();
|
for (Class<?> c : _ancestryCache.keySet()) {
|
if (c.getClassLoader() == webappClassLoader) {
|
toRemove.add(c);
|
}
|
}
|
for (Class<?> c : toRemove) {
|
_ancestryCache.remove(c);
|
}
|
}
|