Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1059

Memory leak in ClassAncestry

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.11.3
    • BSON
    • 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);
      	}
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            coffee_machine Michel Yerly
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: