繁体   English   中英

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

[英]MongoDB / C# Driver and Memory Issue

我正在使用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

具有相同10.000的.Net 4.0 Web应用程序的输出结果为光标:

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

结果:

Bug被报告给10gen,他们将在版本> = 1.4 (他们目前在1.2上工作)修复它!

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

MongoServer类

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

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

我建议使用依赖注入统一结构图 ,..)在容器登记MongoServer实例作为singletone或只是使用经典singletone图案为MongoServer。 如果你是依赖注入的新手,你可以看一下Martin Fowler的文章

更新:

可能问题不在于mongodb c#driver。 我做了以下测试:

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次测试,服务器根本不吃内存。 所以...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM