Fixing the memory leaks in BlogEngine.NET 1.6.1 Unit test adventures - part 1

The joys of setting up a blog

Published on Tuesday, August 31, 2010 1:25:00 AM UTC in Tools

Hi and welcome to the first post of my new blog. When I thought about having a blog, I had some things in mind I wanted to blog about; however, I had no idea what the initial blog post should look like. That changed rapidly when I tried to set up my own blog engine, and so the first post will be a description of my struggles with SubText and BlogEngine.NET.

When I had to choose a blog engine to use, I not only checked for the features I wanted, but also tried to get an impression of the activity of the project as well as the provided support. That ruled out some of the candidates, and after using some demo installations, in the end that left me with SubText and BlogEngine.NET. Mostly because I had used it before in a hosted environment, I chose SubText (version 2.5).

However, setting it up myself was harder than I thought. First I had to correct some of the configuration settings because all I kept getting was error messages from IIS. It's probably not SubText that is to blame for this, because most likely I used different settings in IIS than intended (like the .NET version and application pool settings). But after this was fixed, I couldn't make it use the included database file. Again that wasn't a big issue for me, because I intended to use a separate database for it anyway. After configuring the database, the installation finally was ready to go. I chose one of the provided skins and looked through the features to configure the blog. Unfortunately, I quickly stumbled upon various glitches in the skin, mostly of visual nature. That was the point where I dove into editing CSS to make the layout look consistent and what I wanted it to be.

When I tested the comment function, I came across another bug, and I wasn't able to fix that. When you add a comment and forget to insert a required field (like the name field in the following screenshot)...

... then the captcha disappears when you try to submit your comment:

Which means that even when you correct the missing information, submitting the comment will result in another error because you cannot enter the captcha... I gave up looking for how to fix that after a while and hoped for a fix in the next release. Not pretty, but also not a vital problem.

The next thing however I came across was the search function, which in my eyes is badly broken. I had added some sample posts with code snippets, and when I tested the search, I typed in the word "into", because I knew one of the posts had it in a Linq query snippet. Here's what I got:

Wow, now that's not pretty. Apparently SubText is using Lucene, and Lucene wasn't happy with my simple search term "into" and threw an exception. I found some posts in the SubText forums from people who have similar problems, but only when they used special characters like parenthesis.

Long story short, half an hour later I found myself debugging through SubText's code. I don't have very much experience with Lucene, so I had a bit of a hard time finding the problems. One of it (including the error above) wasn't even Lucene's fault, but how SubText internally pre-processes the entered search terms before they are handed over to Lucene. Also, that routine missed to mask characters which have a special meaning to Lucene. I bit the bullet and fixed all of these, and after working a bit more with the Lucene configuration, like editing the stop words lists and trying different tokenizer, it didn't throw exceptions anymore. However, I didn't get a lot of hits either. Especially all source code snippets seemed to be non-indexed. When a code fragment contained "MyControl.Visibility = Visibility.Collapsed;", for example, then a search for "Visibility" didn't return any results. I checked with the unaltered default installation (where I had not fiddled with Lucene) - same results there. That was kind of disappointing.

A (not so literal) quote I know says: "What separates a good programmer from the average is the will to throw something away he has put blood and sweat into." I tried to be a good programmer at that point :), did a step back, looked at the mess and decided to throw it all out. Although I had spent almost half a day for this, I decided to abandon SubText and try BlogEngine.NET instead.

Setting up BlogEngine.NET went a lot smoother. I downloaded version 1.6.1, and since I did not have any problem using the XML provider instead of a database (trusting that the export/import functions work should a switch ever become necessary), I only had to fix some similar configuration issues as with SubText. To my surprise, the default theme that comes with BlogEngine.NET is very clean and doesn't have any visual glitches in it (I'm still using it at the moment and might customize it in the future). There really are only a few things I had to change, for example the link to the author at the bottom was pointing to a wrong URL. Search worked as expected, and so did everything else.

I already had set up the whole blog locally as I wanted it to be, when I noticed something not so funny. Apparently BlogEngine.NET had some leaks that slowly ate my computer's memory. Slowly as in 600k per post view. Wow. A quick search in their forums found very recent posts from people who had the same problems. Oh noes! :) I had no desire for application pool recycling every ten minutes, so half an hour later I found myself debugging through the second open source project on that day grunt. Unfortunately a profiling session didn't result in clear pointers what was going wrong, so I went the manual route and started looking for the usual suspects in that cases. First, forgotten event handlers. And bam, a few minutes later I had already found three occurrences of events that were hooked on instantiation and never released, probably leading to a lot of control instances on the server that never cleaned up (on each request).

After fixing these issues (more details on those in a second post), an extensive test showed that no more leaking occurred. Cool. So after only one day of installation, configuration, debugging and fixing code, I was ready to upload my new blog, and that's what you see here...

Tags: BlogEngine.NET · Memory Leak · SubText