The Inquisitive Coder – Davy Brion's Blog

Trying to walk that thin line between intelligence and ignorance

The Joys Of Debugging ASP.NET Memory Leaks

Posted by Davy Brion on April 6th, 2009

One of my coworkers describes a very interesting memory leak he just fixed. Be sure to read his post to get the entire story.

We basically had a custom UserControl which contained (among other things) a Repeater which would render another custom UserControl. Nothing really weird there, right? The problem was that the custom UserControl that was created by the Repeater explicitly needed to be disposed. No problem, System.Web.UI.Control implements IDisposable so we could just override the Dispose method, perform our cleanup there and then proceed with the base Dispose implementation.

Now, i’m far from an ASP.NET expert but i was under the impression that all Page and UserControl instances would always have their Dispose method called if they were created by other ASP.NET controls (containing pages or controls). After all, what’s the point in implementing IDisposable in the base Control class if you’re not even going to guarantee proper usage of the pattern, right?

Well, as Kristof describes in his post, there is at least one situation where a UserControl is not disposed of, even though it was created by a Repeater. In our case, the leak was subtle. It took a stress test of 10 hours with a load that would be comparable to real world usage of one month to expose the leak. A lot of people would say “oh, let’s just recycle the application pool periodically and everything is A-OK”. But the very idea of having to recycle an application pool periodically to keep a system up and running is just a cop out to me. Good code should not require periodic restarts. Period.

I wonder how many ASP.NET WebForms applications might actually have the same (or at least similar) problem(s) like the one Kristof encountered. If everyone keeps recycling their application pools periodically, we unfortunately may never found out.

2 Responses to “The Joys Of Debugging ASP.NET Memory Leaks”

  1. The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » Guaranteeing Disposal Of UserControls In ASP.NET Says:

    [...] The Joys Of Debugging ASP.NET Memory Leaks [...]

  2. anon Says:

    10 hour stress test simulating a month’s traffic to reproduce your subtle leak? Doesn’t sound that bad. Try never recycling the app pool in a large SharePoint app. ;)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>