C# MongoDB/C驱动程序和内存问题

C# MongoDB/C驱动程序和内存问题,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我使用的是MongoDB1.8.2Debian和MongoCSharp驱动程序1.1.0.4184 IIS 7.5/.NET4.0x64 每秒在一个具有~3000000个~1.9 GB对象的现有集合中插入多个项。 每次插入->后,Web服务器的内存都会增加~1MB,这会导致非常快的内存使用率>2GB。 内存不再释放,只有应用程序池回收才能释放内存 有什么想法吗 MongoServer server = MongoServer.Create(mongoDBConnectionString); Mo

我使用的是MongoDB1.8.2Debian和MongoCSharp驱动程序1.1.0.4184 IIS 7.5/.NET4.0x64

每秒在一个具有~3000000个~1.9 GB对象的现有集合中插入多个项。 每次插入->后,Web服务器的内存都会增加~1MB,这会导致非常快的内存使用率>2GB。 内存不再释放,只有应用程序池回收才能释放内存

有什么想法吗

MongoServer server = MongoServer.Create(mongoDBConnectionString);
MongoDatabase database = server.GetDatabase(dbName);                

MongoCollection<Result> resultCollection = database.GetCollection<Result>("Result");
resultCollection.Insert(result);
更新:

我的问题不是插入,而是要调查的选择代码库-

我用以下示例代码复制了它:

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");
}
.Net 4.0 Web应用程序的输出在游标中具有相同的10.000结果:

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
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
结果:

错误已报告给10gen,他们将在版本>=1.4中修复它。他们目前正在使用1.2

根据,您应该为连接到的一台服务器创建一个MongoServer实例:

MongoServer类

此类用作使用MongoDB的根对象 服务器您将为您使用的每台服务器创建一个此类实例 连接到

因此,您只需要一个MongoServer实例。这将解决您的内存泄漏问题

我建议用。。要在容器中将MongoServer实例注册为singletone,或者只使用MongoServer的经典模式。如果你是依赖注入方面的新手,你可以看看MartinFowler

更新:

可能问题不在mongodb c驱动程序中。我做了以下测试:

w7,iis 7.5,相同的c驱动程序,相同的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
   });
}

我甚至运行了3次这个测试,服务器根本不消耗内存。所以…

在每批插入之后,您是否要关闭MongoServer/MongoDatabase连接?无需关闭MongoServer/MongoDatabase连接,因为到mongodb驱动程序句柄的实际连接是通过内部连接池自动完成的。不,我没有关闭连接。我觉得这是个bug,我建议将其报告给10gen。我将其更改为单例模式,但行为相同,因为MongoServer.Create将只创建一个实例。MongoServer文档。创建->创建新实例或返回现有MongoServer实例。每种服务器设置组合只创建一个实例。@mac866:好的,我明天会做一些测试,并让您知道。请制作一个aspx页面,在您的集合中只插入一行,并通过wget等多次调用此页面,如果可能的话,请与并发用户一起调用。我对您的场景1请求(包含3000000个插入)也没有问题。谢谢!!!
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
   });
}