Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 - WebDev Server Leaking Memory with Fluent NHibernate?

I have an ASP.NET MVC 3 app that has an MS SQL Server 2008 remote database, connected via Fluent NHibernate. I have another app which is making various GET requests to a URL that triggers a new item being added to the database. Everytime an item is added, my local web server's memory is growing by about 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");
    }

I know this isn't the ideal way to add items to a database, but this is just a test of some other functionality. After about 1000 calls to this method, the server is taking up over 1gb of data! Shortly after, I run out of memory and it crashes. It doesn't make much sense, since all of the items should be getting garbage collected. Is there something I'm missing here?

like image 745
Wesley Tansey Avatar asked Dec 06 '25 23:12

Wesley Tansey


1 Answers

The easiest way to find the problem is to use some memory profiler, they will show you which objects are staying in the memory and who's holding them:

  1. MemProfiler, paid, http://memprofiler.com/

  2. CLR Profiler, free, 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 . And here is a documentation for it - http://msdn.microsoft.com/en-us/library/ff650691.aspx

like image 144
zihotki Avatar answered Dec 10 '25 11:12

zihotki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!