IIS 和 IIS 快速内存泄漏
我有 ASP.NET MVC 3 网站。当在本地 VS Web 服务器或 IIS Express 上运行它时,就可以了。但是当在 IIS (IIS 7.5 Windows 2008 R2) 上运行它时,似乎内存泄漏,因为内存使用量一直在增长。有什么想法吗?
还有一个更新:应用程序中有这样一段代码:
SqlConnection conn = new SqlConnection { //creating connection here };
conn.Open();
SqlCommand command = conn.CreateCommand();
try
{
var reader = command.ExecuteReader();
while (reader.Read())
{
//read the data
}
}
finally
{
conn.Close();
}
也许应该有类似 reader.Dispose 的东西?会不会是内存泄漏的原因?
更新:由于某种原因 gc.Collect 修复了该问题。但这并不是解决办法,因为一直调用 gc.collect 是一个坏主意。
I have and asp.net mvc 3 web site. When running it on local VS web server or IIS express it`s ok. But when running it on IIS (IIS 7.5 Windows 2008 R2) it seems like memory leak as memory usage is growing all the time. Any ideas?
One more update: there`s such a code in the app:
SqlConnection conn = new SqlConnection { //creating connection here };
conn.Open();
SqlCommand command = conn.CreateCommand();
try
{
var reader = command.ExecuteReader();
while (reader.Read())
{
//read the data
}
}
finally
{
conn.Close();
}
Maybe there should be something like reader.Dispose? Can it be the cause of memory leak?
UPDATE: for some reason gc.Collect fixes the issue. But it`s not hte way out as calling gc.collect all the way is a bad idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的最佳实践是使用括号,这是 c# 的一个很棒的功能。当您将“using”关键字与括号一起使用时,当超出括号范围时,它会自动处理使用过的对象。这是一个例子;
这里还有一个微软链接,上面写着“连接在 using 块结束时自动关闭”。 http:// /msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close%28v=VS.85%29.aspx
我希望它有帮助。
well best practice here would be to use brackets, this is a great feature of c#. when you use the "using" keyword with a bracket, it would dispose the used object automatically, when running out of the bracket scope. here's an example;
Here's also the microsoft link that says "The connection is automatically closed at the end of the using block." http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close%28v=VS.85%29.aspx
I hope it helps.