ASP.NET MVC 3 - WebDev服务器使用流畅的NHibernate泄漏内存?

Wes*_*sey 3 fluent-nhibernate asp.net-mvc-3

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

    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");
    }
Run Code Online (Sandbox Code Playgroud)

我知道这不是向数据库添加项目的理想方式,但这只是对其他一些功能的测试.在大约1000次调用此方法后,服务器占用了超过1GB的数据!不久之后,我的内存不足而且崩溃了.它没有多大意义,因为所有的项目都应该被垃圾收集.这里有什么我想念的吗?

zih*_*tki 5

找到问题的最简单方法是使用一些内存分析器,它们将显示哪些对象留在内存中以及谁持有它们:

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

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