【发布时间】:2018-07-11 20:54:51
【问题描述】:
我们项目中有一个webAPI,很简单,但是执行后不会释放内存,每次执行都会增加使用的内存。
我使用 webAPI 2.0 和 MongoDB 作为后端服务器。
public class LogsController:ApiController
{
BsonDocument __docs;
IMongoDatabase __db;
IMongoCollection<BsonDocument> __docsColl;
[Route("api/Logs")]
public async Task<int> Post(RequestData logs)
{
if (logs.Token == "I")
{
__db = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("serverIP", 27017),
Credentials = new[] { MongoCredential.CreateCredential("database", "user name", "password") }
}).GetDatabase("connect_database");
__docs = new BsonDocument()
{
{ "Customer",logs.Customer}
};
__docsColl = __db.GetCollection<BsonDocument>("InsertData");
await __docsColl.InsertOneAsync(__docs);
}
logs = null;
return 1;
}
protected override void Dispose(bool disposing)
{
__docs = null;
__db = null;
__docsColl = null;
GC.SuppressFinalize(true);
base.Dispose(disposing);
}
}
我已经尝试了所有可能的解决方案,但到目前为止都没有运气。
【问题讨论】:
-
it is not release memoryevery time it will increase the memory您是如何得出这些结论的?是否有可以发布的日志或执行结果? -
您不要在任何地方调用“Dispose”方法。
-
声明3个全局变量有什么具体原因吗?它们是否在这个类中的任何地方使用,在
Post()方法之外? -
由于 webAPI 实现了 IDisposable,所以它会在我使用调试检查的 webAPI 响应后自动调用,它会调用 Dispose 方法。
-
移除 Dispose(),将属性移入 Post()。去掉“...= null”,报告结果
标签: c# asp.net-mvc-4 asp.net-web-api asp.net-web-api2 mongodb-.net-driver