Memory profiling from the trenches

When you’re in school you learn that the .NET framework takes care of all that nasty memory management. From the moment you’re past all those small exercises and develop “real” software you quickly see that if you don’t follow some guidelines the garbage collector fails to reclaim memory.

The past week I’ve spent quite some time using ANTS Memory Profiler to look for leaks in a big project at work. It’s a Windows forms application which talks to online webservices. It was pretty clear from the start that any form that was opened remained in memory since the total memory footprint increased and never dropped. Using ANTS you can easily see how much instances of which class are active, how they are related and how much space they occupy. Rather fast I found that a lot of eventhandlers were keeping hold of the forms or presenters (we use MVC to create our UI) hence the memory footprint.

It’s a good practice that whenever you subscribe to an event, you also unsubscribe when you no longer need to know about it. Every += needs its -=. In some places its easy to spot. When you got one presenter, let’s see an OrderPresenter with one view, OrdersView, you quickly see the mistake that was made. But in more complex situations, let’s say that OrdersPresenter was also talking to an CustomerPresenter and OrderDetailPresenter which in turn talked to other presenters you’ll be switching between Visual Studio and ANTS to try to understand what’s going on in the system.

Never the less, I was able to get all the forms disposed properly when they were closed or no longer needed. Except in one specific situation. The application has a multiple document interface (MDI). When it starts it loads the OrdersOverview form and shows some global information, in this example all current orders. When I closed this overview screen and looked at the output from Ants the form was still in memory. No matter what I tried, for some reason the MDI parent kept referring to the MDI child which was closed. After hours of walking through the code to find the culprit I launched one Google query which found a page on Microsoft Connect. It’s a bug in the .NET framework, sigh, what a waste of time.

Another thing that was new to me was that whenever you show a form as modal and you close it. It will be kept active as well, you need to explicitly call Dispose to get rid of it. Four years of WinForm development and I still learn so much new stuff.

SVN Repository available

I’ve made availble a svn repository with the source code of the channel library for Spring.Net and my work on a Cuyahoga module for events. The last one is still pretty much a work in progress. I think I’ll be able to work on it some more later this year. There’s a lot on my todo list.

Readers have reported that the latest version of Fluent NHibernate breaks the approach which I illustrated on this blog, I’ll look into that later today. I’ll also be patching the channel library with a bug fix which someone on the Spring.Net forum was kind enough to provide. Had hoped to do that yesterday but the machine on which I was working did not have NUnit installed and Sourceforge was down. So stay tuned, this blog will get some more action in August.

Repository is located at:

Edit: Moved code to github @ https://github.com/BennyM/SpringWCFChannelManager

Taking babysteps into Ruby on Rails land

It’s been real quiet here because I’ve been rather busy at work and the re-enactment season kicked off. I’ve also started a little project for my own which will be based on Ruby on Rails. I have no experience with it what so ever.

I followed the steps on the offical site, please note that you need super user privileges so begin each command with sudo. While everything was installing I stumbled upon this article on the Apple site, which I’m now following to understand and learn the language and the framework. It’s a year old but seems up to date so far. They also specify the sudo command so you’re ok with my first tip ;).

MacBook powering two external displays

I tend to hook up my MacBook on one of my external displays when I sit at my desk. I have two 22″ screens, one which I use for the MacBook the other I used for my desktop PC. A while ago I gave that one away and so the other screen was only gathering dust.

The MacBook only has one external display port so in order to hook up two displays you need to do some research. One possible solution is the DualHead2Go from Matrox. You hook this up on your display port and it basically directs half of the signal to one screen and half to the other. So you need to set your resolution to a rather big one, the MacBook is not powerfull to power two displays at 1680 * 1050. It wil also set you back around 300$/€.

Then there’s ViDock, a very powerfull solution but it comes with a powerful price, almost a new laptop (700$/€).

And then there’s DisplayLink‘s technology, which enables displays to connect via USB. They’ve been around for some time and now have drivers supports Mac OS X. I went for this solution from EVGA, compared to the others it’s relativly cheap at around 70$/€ for their best model (UV-16 +). Which can power displays up to 1680 * 1050, just the resolution I wanted. While it can’t play HD content smoothly, normal usage is just fine.

This week on my screen #7

Updated to BE 1.5

Just a small post to see if everything is still up and running, only the lightbox extenstion is having a small design issue. Apart from that everything went smooth. For those using MS SQL 2000 as backend, you can just use the upgrade script that comes with the 1.5 release.

Mono 2.4 and MonoDevelop 2.0 on OSX

I installed Mono and MonoDevelop again this week but found out rather quickly that not much has changed in MonoDevelop since the last time I used it. MD went 2.0 recently while Mono is now 2.4.

I probably should have guessed that all the problems I ran into using MD were still not resolved because of the red letters on their site discouraging using MonoDevelop on the Mac. On the forums however I found out that in the next version a lot of improvements should make it into the Mac distribution.

Mono is closing in on .Net, it was good to see that a lot of the c# language enhancements as well as new .NET features have made it into the platform. Including automatic properties, lambdas, etc. Linq is said to be supported as well but I was unable to write even a simple query. I never got the select statement to work. Could be me though.

What I did find interesting was how you can create native looking applications for almost every platform while using the same Mono core of your application. I really want to try that out! On the Mono site you get linked to Cocoa# for OSX but that project seems to be rather silent though a user made a recent contribution. I did like the way on how you could use the existing development tools available, xCode and Interface builder so you would not need to learn another application. There’s a video demonstrating the approach.

A more active project, with the latest version released in March, is Monobjc which seems to have the same goal as Cocoa#. I’ll probably try this one out first. Anyone with some Objective-C and Cocoa experience will recognize the way it works in this tutorial. Though at first glance you can’t use xCode to write your c#.

Oh yeah last time I checked you can use Spring.Net with Mono too.