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

标签 c# mongodb mongodb-.net-driver

我正在使用 MongoDB 1.8.2 (Debian) 和 mongo-csharp-driver 1.1.0.4184 (IIS 7.5/.Net 4.0 x64)。

在现有集合中每秒插入多个项目,其中包含约 3,000,000 个对象(约 1.9 GB)。
每次插入后,WebServer 内存都会增加约 1 MB -> 这导致内存使用量非常快 > 2 GB。
内存不再释放,只有应用程序池回收才能释放内存。

有什么想法吗?

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

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

结果看起来像

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

更新:

我的问题不是插入,而是选择 - 抱歉奇怪的代码库要调查 ;-)

我用这个示例代码复制了它:

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 控制台应用程序的输出,光标中有 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

来自 .Net 4.0 Web 应用程序 的输出具有相同的 10.000 个结果:

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)!!!

最佳答案

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

MongoServer class

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

所以你只需要一个 MongoServer 实例。这应该可以解决您的内存泄漏问题。

我建议使用 dependency injection ( unity , structure map , ..) 在容器中将 MongoServer 实例注册为单例或只使用经典的 singletone MongoServer 的模式。如果您是依赖注入(inject)的新手,可以查看 Martin Fowler article .

更新:

可能问题不在 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 次这个测试并且服务器根本不吃内存。所以...

关于c# - MongoDB/C# 驱动程序和内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6911530/

相关文章:

MongoDb:使用字符串作为 Id

c# - 正则表达式检查文件格式是否符合 C# 中的预期

c# - 如何在谷歌日历中创建 "recurData"?

c# 如何检查队列是否包含具有特定值的对象属性?

python - MongoDB Compass 中的 UTC 日期不正确

c# - 2.0 驱动程序中的 MongoServer.State 等效项

c# - Mongodb C# 批量更新/替换子集合

c# - 如何在 C# 中创建一个将生成 HTTP 响应主体行的迭代器?

mongodb - 是时候学习 Cassandra/MongoDB

mongodb - mongo 控制台打开时出现 Mlock 错误 - mlock : Cannot allocate locked memory 失败