2011-02-19 79 views
3

我有一个ASP.NET MVC 3应用程序,它有一个MS SQL Server 2008远程数据库,通过Fluent NHibernate连接。我有另一个应用程序正在向URL发出各种GET请求,这会触发将新项目添加到数据库中。每次添加项目时,本地Web服务器的内存都将增加大约100k。ASP.NET MVC 3 - WebDev服务器泄漏内存与流利的NHibernate?

public ActionResult AddItem(string text) 
    { 
     using (var DatabaseSession = new FluentDatabase().Session) 
      using (var tx = DatabaseSession.BeginTransaction()) 
      { 
       Item item = DatabaseSession 
          .QueryOver<Item>() 
          .Where(x => x.Text == text) 
          .SingleOrDefault(); 
       if (item == null) 
         item = new ... // initialize 

       item.Text = text; 

       DatabaseSession.SaveOrUpdate(item); 
       tx.Commit(); 
       DatabaseSession.Flush(); 
      } 

     return RedirectToAction("Index"); 
    } 

我知道这是不是将项目添加到数据库的理想方式,但是这仅仅是一些其他的功能测试。经过约1000次调用此方法后,服务器占用了1GB以上的数据!不久之后,我耗尽内存并崩溃。它没有多大意义,因为所有的项目都应该被垃圾收集。有什么我在这里失踪?

+0

你不需要在事务中调用Flush,你只需要调用commit。 – Phill 2011-02-19 00:43:53

+0

我也这么认为,但是当我没有调用flush时,它并没有更新我的数据库。 – 2011-02-19 00:45:08

回答

5

找到问题的最简单方法是使用一些内存设置,他们会告诉你哪些对象是住在内存和谁在阻碍他们:

  1. MemProfiler,支付,http://memprofiler.com/

  2. CLR Profiler,免费,.Net 4的Microsoft CLR Profiler - http://www.microsoft.com/downloads/en/details.aspx?FamilyID=be2d842b-fdce-4600-8d32-a3cf74fda5e1,.Net 2和3.5的CLR Profiler - http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0。这里是它的文档 - http://msdn.microsoft.com/en-us/library/ff650691.aspx