New issue 385 by m...@thelevines.com: Memory leak of MappedClass when using
BasicDAO
http://code.google.com/p/morphia/issues/detail?id=385
What version are you using? (Morphia/Driver/MongoDB)
morphia-0.99.1-SNAPSHOT
mongo-2.7.2
I see a leak of MappedClass and the associated MappedFields when using
BasicDAO. I've simplified it down to simply creating an instance of a
class that extends BasicDAO. Running GC and collecting a heap dump shows
an extra MappedClass in the heap with every call to doGet in the code
below. Leaving all code as is, but modifying the DAO to NOT extend
BasicDAO and rather access the Datastore directly (w/ the instance
retrieved from the MongoManager) clears up the leak.
public class TesterDao extends BasicDAO<Tester, ObjectId> {
public TesterDao() {
super(Tester.class, MongoManager.instance().getDb());
}
}
public class MorphiaTestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
TesterDao testerDao = new TesterDao();
}
}
public class MongoManager {
private static final MongoManager INSTANCE = new MongoManager();
private final String MONGO_HOST = "localhost";
private final int MONGO_PORT = 27017;
private Datastore theDb = null;
private MongoManager() {
Mongo mongo;
try {
mongo = new Mongo(MONGO_HOST, MONGO_PORT);
theDb = new Morphia()
.map(Tester.class)
.createDatastore(mongo, "MorphiaTest");
theDb.ensureIndexes();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
public static MongoManager instance() {
return INSTANCE;
}
public Datastore getDb() {
return theDb;
}
}
@Entity (noClassnameStored=true)
public class Tester {
@Id
private ObjectId id;
private String name;
public Tester() {}
public Tester(String name) {
this.name = name;
}
public ObjectId getId() {
return id;
}
public String getName() {
return name;
}
}