BlogEngine.NET memory leak fix

Jul 8, 2008

My pride takes a hit every time I read the title of this post. At least it contains the word “fix”.

People have reported a memory leak on a few occasions, but I’ve never been able to find it and neither have anyone else. I just left it because it has never bothered me. It might be because of a very forgiving hosting partner that just adds more RAM if necessary and never tell me.

Yesterday evening I found myself in the right mood for tracking down this leak and it only took me 6 hours to find it. The fix took about 8 seconds, but what jolly 8 seconds that was.

The problem

After six hours I found that the leak was in the Related Posts server control. The constructor registered an event handler for the Post.Saved event, but the constructor wasn’t static like all the other controls. It was public which means that at every page view the event handler would be registered. That resulted in that the control never got disposed because of the reference to the event. So for each page view, an equal number of controls got stuck in memory and couldn't be garbage collected.

The fix

Make the constructor and event handler static so they only run once in the application life cycle. Let this be a lesson to all, remember to unhook your event handlers. The fix works for BlogEngine.NET 1.3, 1.3.1 and 1.4 and you just have to replace one file in the App_Code folder.

Download the memory leak fix at CodePlex and follow the instructions in the readme.txt.

I’m sorry it took so long to find this fix. I hope it hasn’t caused you too much trouble.

BlogEngine.NEXT

In the light of this and other bugs, we have decided to do a service release in a couple of weeks. Beside the memory leak fix, it will include many other tweaks, fixes and a few new minor features. It will be 100% backwards compatible with the current 1.4 release. If you have found some issues with 1.4 that you want fixed in the next release, please add them to the issue tracker.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (16) -

Egil
Egil Denmark
7/8/2008 9:08:08 PM #

Hi Mads

It would be great if you could do a post on the (general) issue and provide some examples.

If I for example have a user control that exposes an event, and the page that hosts the user control registers to the event in page_OnLoad on every request, will that lead to the same memory leak issue you have been experiencing?
Do I need to manually un-register to the event during page_PreRender or will the asp.net runtime do that for me automatically?

Regards, Egil.

Chris Pietschmann
Chris Pietschmann United States
7/8/2008 9:10:27 PM #

Memory leaks are never fun, but at least it was easy to fix once you found it. Thanks.

Troy Goode
Troy Goode United States
7/8/2008 9:46:09 PM #

Nice job tracking down a pretty obscure issue. For the past few months I've had to throttle the app pool my blog is in because the memory would eventually grow out of hand, so I'll certainly be implementing this ASAP. Here's to less application restarts!

Scott Marlowe
Scott Marlowe United States
7/9/2008 12:14:42 AM #

Cool. Glad you found it and thanks for the persistence. Of course, I'll hold off on my migration to 1.4 until all the tweaks are in. I can wait 2 more weeks. Wink

Chad Thiele
Chad Thiele United States
7/9/2008 12:59:18 AM #

Can you tell us what the issues are that you'll be working on in the service release?

Ali Özgür
Ali Özgür Turkey
7/9/2008 4:40:26 AM #

After applying the fix I've got compilation error. Here is the error
  
'BlogEngine.Core.Utils' does not contain a definition for 'StripHtml'. This error is in Line 139 in RelatedPosts.cs

Steve
Steve
7/9/2008 1:06:07 PM #

i got the same compilation error:


Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0117: 'BlogEngine.Core.Utils' does not contain a definition for 'StripHtml'

Source Error:



Line 137:            if (String.IsNullOrEmpty(description))
Line 138:            {
Line 139:              string content = Utils.StripHtml(post.Content);
Line 140:              description = content.Length > DescriptionMaxLength ? content.Substring(0, DescriptionMaxLength) + "..." : content;
Line 141:            }


Source File: d:\hosting\App_Code\Controls\RelatedPosts.cs    Line: 139

Mads Kristensen
Mads Kristensen Denmark
7/9/2008 4:22:33 PM #

Sorry for the Utils.StripHtml error. It has now been fixed and uploaded to CodePlex.

Mark Brackett
Mark Brackett United States
7/10/2008 12:05:47 PM #

It would be interesting to know *how* you found the source of the leak. Once found, the fix is usually pretty mundane. ;)

m3rlinez
m3rlinez Thailand
7/11/2008 5:28:45 AM #

This really reminds me of the Princeton DARPA team's case. The mem leaks are caused from registered event handlers. www.codeproject.com/.../...edUsedANTSProfiler.aspx

Mike Borozdin
Mike Borozdin Russia
7/19/2008 9:08:50 AM #

Yesterday evening I found myself in the right mood for tracking down this leak and it only took me 6 hours to find it. The fix took about 8 seconds, but what jolly 8 seconds that was.

Standard! This is how it always goes. It's always harder to spot an error than to fix it.

Ivan | SEO Konzultant
Ivan | SEO Konzultant Croatia
7/21/2008 9:25:24 AM #

Is there a XML Sitemap Generator for the BlogEngine.NET (as a part of it or as an Extension) please?

Thanks,
Ivan

Mads Kristensen
Mads Kristensen United States
7/21/2008 9:54:46 AM #

@Ivan,

Yes there is. It's not an extension though. You find it at /sitemap.axd. Mine is here http://blog.madskristensen.dk/sitemap.axd

Ivan | SEO Konzultant
Ivan | SEO Konzultant Croatia
7/21/2008 12:03:11 PM #

Thanks Mads!

Ivan

Ali Özgür
Ali Özgür Turkey
8/12/2008 5:13:27 AM #

Hi Madds,
After applying RelatedPosts fix I got some strange styling behaviour for my post comments. I guess RelatedPosts.cs CreateList method strips the description of the related post so that it produces non well formed html string.

You can check blog.pragmasql.com/.../...Burrow-contribution.aspx
for that strange behaviour.

When I disable related posts feature everything is fine.

Mr. Phucked
Mr. Phucked
8/20/2008 7:46:28 PM #

Glad you found it, it was crashing my host every 2 hours!!

Pingbacks and trackbacks (2)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.