I am currently working with Java, MongoDB and several threads.
For each threads, I run MongoDB connection.
private Mongo m;
m = new Mongo();
I had a look to the memory usage with TOP and I saw multiple instances of MongoDB Jar
(call to library mongo-2.6.3.jar). I tried to destroy it after end of processing of each thread with :
m.close();
m = null;
System.gc();
But nothing instances are still in memory until my program is stopped.
Any ideas ? Thanks.
OK I have made some tests regarding the tutorial, but not very successful. In fact, I have :
public class process {
public static Mongo m;
public static DB db;
public process() {
m = new Mongo();
//for each database
threadDatabase thread = new threadDatabase(database);
thread.start();
}
}
public class threadDatabase extends Thread {
public threadDatabase (String database) {
this.database = database;
}
public void run() {
process.db.requestStart();
processDatabase(this.database);
process.db.requestDone();
}
private boolean processDatabase(String dbname) {
process.db = process.m.getDB(dbname);
//g开发者_如何学Cetting data on the dedicated database
}
}
From the Java Tutorial about MongoDB
The Mongo object instance actually represents a pool of connections to the database; you will only need one object of class Mongo even with multiple threads. See the concurrency doc page for more information.
Nulling references and calling System.gc
does not guarantee to free up or garbage collect any resources at all. Fur further information see this question.
My advice: Only use one instance of Mongo
per VM, the rest will be handled automatically.
精彩评论