Skip to content

Commit

Permalink
Clearing hibernate session factories that leak memory on spring refresh:
Browse files Browse the repository at this point in the history
Investigate possible memory leak in 1.9.0 or one of its bundled modules.
- TRUNK-3440
  • Loading branch information
dkayiwa committed Apr 30, 2013
1 parent af6addf commit ffc9294
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/src/main/java/org/openmrs/util/MemoryLeakUtil.java
Expand Up @@ -14,10 +14,12 @@
package org.openmrs.util;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.impl.SessionFactoryObjectFactory;

import sun.net.www.http.HttpClient;
import sun.net.www.http.KeepAliveCache;
Expand Down Expand Up @@ -81,4 +83,24 @@ public static void shutdownKeepAliveTimer() {
log.error(e.getMessage(), e);
}
}

/**
* Clears the hibernate session factories cached in the SessionFactoryObjectFactory
*/
public static void clearHibernateSessionFactories() {
try {
Field field = SessionFactoryObjectFactory.class.getDeclaredField("INSTANCES");
field.setAccessible(true);
Map instances = (Map) field.get(null);
instances.clear();

field = SessionFactoryObjectFactory.class.getDeclaredField("NAMED_INSTANCES");
field.setAccessible(true);
Map namedInstances = (Map) field.get(null);
namedInstances.clear();
}
catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
}
}
2 changes: 2 additions & 0 deletions api/src/main/java/org/openmrs/util/OpenmrsClassLoader.java
Expand Up @@ -323,6 +323,8 @@ public static void destroyInstance() {
log.error(ex.getMessage(), ex);
}
}

MemoryLeakUtil.clearHibernateSessionFactories();

OpenmrsClassScanner.destroyInstance();

Expand Down

0 comments on commit ffc9294

Please sign in to comment.