Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.

Silverlight Memory Leak DataGrid, DataForm, DataTemplate, etc...

Silverlight 4 is a fantastic platform. Unfortunately it suffers from memory leaks on some of the standard controls if they are created in a certain way and some third party controls will leak memory.

On the Silverlight forums there is a thread that can be found here:

http://forums.silverlight.net/forums/p/171739/466567.aspx#466567

For example, if the XAML is created like this:

    <Grid x:Name="LayoutRoot" Background="White">
        <controls:DataGrid AutoGenerateColumns="False">
            <controls:DataGrid.Columns>
                <controls:DataGridTemplateColumn>
                    <controls:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBlock Text="TextBlock"/>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellEditingTemplate>
                </controls:DataGridTemplateColumn>
            </controls:DataGrid.Columns>
        </controls:DataGrid>
    </Grid>

The grid object will never be released in garbage collection due to a bug in the Silverlight engine.

As a work around to this bug, Tim Heuer and his team at Microsoft suggest that the DataTemplate be changed to a Static Resource like this.

    <UserControl.Resources>
        <DataTemplate x:Name="testTemplate">
            <TextBlock Text="TextBlock"/>
        </DataTemplate>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid AutoGenerateColumns="False">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn CellEditingTemplate="{StaticResource testTemplate}" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>

This grid object will be released in garbage collection.

In order to quickly identify a whether a control leaks, I built a Visual Studio 2010 solution which dynamically creates control(s) and adds them to a StackPanel and a WeakReference list. Then when the user clicks Remove, all controls are removed from the StackPanel and a garbage collection is done. The WeakReference list is checked and any controls still “Alive” in the WeakReference list can be considered memory leaks.

Here are the steps to follow:

  • Download the VS 2010 solution from here: http://www.devtoolshed.com/sites/default/files/private/blog_article_file...
  • Open the SilverlightUIElementLeakTester.MainPage.xaml.cs file.
  • Find the ButtonAddItem_Click() method.
  • Note how several controls will be created here and added to MainPage xaml and the ObjectTracker
  •         private void ButtonAddItem_Click(object sender, RoutedEventArgs e)
            {
                AddItem(new TextBox());
                AddItem(new StackPanel());
                AddItem(new ComboBox());
                AddItem(new SilverlightUIElementLeakTester.UserControls.DataGridInlineTemplate());
                AddItem(new SilverlightUIElementLeakTester.UserControls.DataGridStaticTemplate());
    
                // TODO - Add other AddItem calls here to see if UIElement derived controls leak.
            }
    

    * Add any additional UIElement derived controls here that you would like to test for memory leaks.

  • Run the program.
  • Click the Add button to dynamically create all the controls.
  • Click the RemoveAll to remove the items and do a garbage collection. Note you may need to press the button more than once to assure the garbage collector has retrieved all that it can get.
  • Finally you end up with this:
  • ***    Possible memory leaks in the objects below or their children.   ***
    *** Clear memory again and see if any of the objects free from memory. ***
    SilverlightUIElementLeakTester.UserControls.DataGridInlineTemplate
    ----
    GC.GetTotalMemory(true): 362636 Bytes, 0 MB
    

    All objects were released from memory except for the UserControl that has a data grid with an inline datatemplate.

    Note that I have found this solution invaluable for identifying other controls that are leaking in Sivlerlight. I used this solution to figure out that the DevExpress RichEdit box leaks and is never released. I will be opening a support ticket with them shortly.

    Thanks to Davy Bryon’s ObjectTracker class which is included in the solution. His article can be found here: http://davybrion.com/blog/2009/08/tracking-dangling-object-references-in...

    Thanks to http://manoli.net/csharpformat/ for the code formatting.



    Can't download

    Hello,
    I can't download that file. It's forbidden!