This is a mirror of official site: http://jasper-net.blogspot.com/

Finding Memory Leaks In Silverlight With WinDbg

| Sunday, February 12, 2012
As i mentioned in a previous post, you can attach WinDbg to a browser to find memory leaks in your Silverlight applications. I figured it would be a good idea to write down how this process works, since i always end up having to look it up again whenever i need to do this.

I wrote a very simple Silverlight application which has a rather typical memory leak. Here's the actual code:

public event EventHandler<MyEventArgs> MyEvent = delegate { };
 
private void CreateNewViewButton_OnClick(object sender, RoutedEventArgs e)
{
    ViewContainer.Children.Clear();
 
    var newView = new MyView();
    MyEvent += newView.EventHandler;
 
    ViewContainer.Children.Add(newView);
}

For some of you, the memory leak is already very clear. Like i said, it's a very simple example ;)

Let's go through the process of finding and fixing the memory leak using WinDbg. First of all, download Debugging Tools For Windows (which contains the WinDbg executable) here and install it.

Then we start our application in Internet Explorer (for some reason i can't use WinDbg to inspect the managed memory heap with Firefox, so i just use Internet Explorer for this stuff) and use it. In the case of my example, that means clicking the button which creates a new view a couple of times.

Open WinDbg.exe and select the 'Attach to a Process' menu item in the 'File' menu and select the iexplore.exe process.

Then you need to load the correct version of sos.dll:

step1

After that we can see which types of our MySilverlightApplication namespace are present in the managed heap, including how many instances of them:

step2.png


Read more: The Inquisitive Coder - Davy Brion's Blog
QR: http://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://davybrion.com/blog/2009/08/finding-memory-leaks-in-silverlight-with-windbg/

Posted via email from Jasper-net

0 comments: