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:
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.
*** 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 MBAll 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.
Popular Articles
Last viewed:
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- Getting Started with Microsoft Chart Controls for ASP.NET 3.5
- Clustered Index vs. Non-Clustered Index in SQL Server
- C# Download File with Progress Bar
- Deploy Crystal Reports 2008 Run-time on Remote Server
- C# Free Component to Generate PDF - Convert HTML to PDF
Recent comments
- Awsome!!
8 hours 49 min ago - C# insert image
23 hours 42 min ago - jkll
3 days 11 hours ago - Thank You
4 days 11 hours ago - Another approach
6 days 12 hours ago - Issue
1 week 2 hours ago - thanks
1 week 12 hours ago - Calendar date time
1 week 18 hours ago - Nice Explanation
1 week 22 hours ago - ramya
1 week 3 days ago

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