Problems with driver and memory MongoDB / C #

I am using MongoDB 1.8.2 (Debian) and mongo-csharp-driver 1.1.0.4184 (IIS 7.5 / .Net 4.0 x64).

Several items are inserted every second into an existing collection with ~ 3,000,000 objects (~ 1.9 GB).
WebServer memory increases by ~ 1 MB after each insertion →, which leads very quickly to memory usage> 2 GB.
Memory is no longer available, and only a backup of the application pool can free up memory.

Any ideas?

MongoServer server = MongoServer.Create(mongoDBConnectionString); MongoDatabase database = server.GetDatabase(dbName); MongoCollection<Result> resultCollection = database.GetCollection<Result>("Result"); resultCollection.Insert(result); 
Result

looks like

 private class Result { public ObjectId _id { get; set; } public DateTime Timestamp { get; set; } public int Location { get; set; } public string Content { get; set; } } 

UPDATE:

My problem is not in the insert, but in the choice - sorry the strange code base for the study; -)

I reproduced it using this example. The code:

 Console.WriteLine("Start - " + GC.GetTotalMemory(false).ToString("#,###,##0") + " Bytes"); for (int i = 0; i < 10; i++) { MongoServer server = MongoServer.Create(mongoDBConnectionString); MongoDatabase database = server.GetDatabase(dbName); MongoCollection<Result> resultCollection = database.GetCollection<Result>("Result"); var query = Query.And ( Query.EQ("Location", 1), Query.GTE("Timestamp", DateTime.Now.AddDays(-90)) ); MongoCursor<Result> cursor = resultCollection.FindAs<Result>(query); foreach (Result result in cursor) { // NOOP } Console.WriteLine(i + " - " + GC.GetTotalMemory(false).ToString("#,###,##0") + " Bytes"); } 

Exiting .Net 4.0 Console application with 10,000 results in the cursor:

 Start - 193.060 Bytes 0 - 12.736.588 Bytes 1 - 24.331.600 Bytes 2 - 16.180.484 Bytes 3 - 13.223.036 Bytes 4 - 30.974.892 Bytes 5 - 13.335.236 Bytes 6 - 13.439.448 Bytes 7 - 13.942.436 Bytes 8 - 14.026.108 Bytes 9 - 14.113.352 Bytes 

Exiting .NET 4.0 Web applications with the same result of 10,000 in the cursor:

 Start - 5.258.376 Bytes 0 - 20.677.816 Bytes 1 - 29.893.880 Bytes 2 - 43.783.016 Bytes 3 - 20.921.280 Bytes 4 - 34.814.088 Bytes 5 - 48.698.704 Bytes 6 - 62.576.480 Bytes 7 - 76.453.728 Bytes 8 - 90.347.360 Bytes 9 - 104.232.800 Bytes 

RESULT:

A 10gen error has been reported and they will fix it in version> = 1.4 (they currently work on 1.2) !!!

+7
source share
1 answer

According to the documentation, you should create one instance of MongoServer for one server to which you are connected:

Class MongoServer

This class serves as the root object for working with the MongoDB server. You will create one instance of this class for each server on which you connect to.

So you only need one instance of MongoServer. This should solve the memory leak problem.

I suggest using dependency injection ( unity , map structure , ..) to register a MongoServer instance in the container as single or just use the classic singleletone for MongoServer. If you are new to dependency injection, you can take a look at the Martin Fowler article .

Update:

Probably the problem is not the mongodb C # driver. I did the following testing:

w7, iis 7.5, same c # driver, same mongodb:

 for (int i = 0; i < 3000000; i++) { MongoServer server = MongoServer.Create("mongodb://localhost:27020"); MongoDatabase database = server.GetDatabase("mpower_read"); MongoCollection<Result> resultCollection = database.GetCollection<Result>("results"); resultCollection.Insert(new Result() { _id = ObjectId.GenerateNewId(), Content = i.ToString(), Location = i, Timestamp = DateTime.Now }); } 

I even run this test 3 times, and the server does not eat memory at all. So that...

+3
source

All Articles