BlogEngine.NET memory leak fix

by Mads Kristensen 9. July 2008 04:26

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

Tags: , ,

BlogEngine

Comments

7/9/2008 6:08:08 AM #

Egil

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.

Egil Denmark |

7/9/2008 6:10:27 AM #

Chris Pietschmann

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

Chris Pietschmann United States |

7/9/2008 6:46:09 AM #

Troy Goode

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!

Troy Goode United States |

7/9/2008 9:14:42 AM #

Scott Marlowe

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

Scott Marlowe United States |

7/9/2008 9:59:18 AM #

Chad Thiele

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

Chad Thiele United States |

7/9/2008 1:40:26 PM #

Ali Özgür

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

Ali Özgür Turkey |

7/9/2008 3:15:13 PM #

pingback

Pingback from blog.cwa.me.uk

Reflective Perspective - Chris Alcock  » The Morning Brew #132

blog.cwa.me.uk |

7/9/2008 10:06:07 PM #

Steve

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

Steve |

7/10/2008 1:22:33 AM #

Mads Kristensen

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

Mads Kristensen Denmark |

7/10/2008 9:05:47 PM #

Mark Brackett

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

Mark Brackett United States |

7/11/2008 2:28:45 PM #

m3rlinez

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

m3rlinez Thailand |

7/19/2008 6:08:50 PM #

Mike Borozdin

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.

Mike Borozdin Russia |

7/21/2008 6:25:24 PM #

Ivan | SEO Konzultant

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

Thanks,
Ivan

Ivan | SEO Konzultant Croatia |

7/21/2008 6:54:46 PM #

Mads Kristensen

@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

Mads Kristensen United States |

7/21/2008 9:03:11 PM #

Ivan | SEO Konzultant

Thanks Mads!

Ivan

Ivan | SEO Konzultant Croatia |

7/21/2008 9:11:10 PM #

trackback

Trackback from SEO Konzultant

BlogEngine.NET ima XML Sitemap!

SEO Konzultant |

8/12/2008 2:13:27 PM #

Ali Özgür

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.

Ali Özgür Turkey |

8/21/2008 4:46:28 AM #

Mr. Phucked

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

Mr. Phucked |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008