IIS 和 IIS 快速内存泄漏

发布于 2024-12-12 04:58:24 字数 584 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-12-19 04:58:24

这里的最佳实践是使用括号,这是 c# 的一个很棒的功能。当您将“using”关键字与括号一起使用时,当超出括号范围时,它会自动处理使用过的对象。这是一个例子;

// SqlConnection implements IDisposable, will be disposed after bracket is closed
using(SqlConnection conn = new SqlConnection())
{
      conn.Open();
      // SqlCommand implements IDisposable, will be disposed after bracket is closed
      using(SqlCommand command = conn.CreateCommand())
      {
         // DataReader implements IDisposable, will be disposed after bracket is closed
         using(var reader = command.ExecuteReader())
         {
            while (reader.Read())
            { 
              // read here.
            }
         }
      }
}

这里还有一个微软链接,上面写着“连接在 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;

// SqlConnection implements IDisposable, will be disposed after bracket is closed
using(SqlConnection conn = new SqlConnection())
{
      conn.Open();
      // SqlCommand implements IDisposable, will be disposed after bracket is closed
      using(SqlCommand command = conn.CreateCommand())
      {
         // DataReader implements IDisposable, will be disposed after bracket is closed
         using(var reader = command.ExecuteReader())
         {
            while (reader.Read())
            { 
              // read here.
            }
         }
      }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文