Reply to comment
Silverlight Memory Leak DataGrid, DataForm, DataTemplate, etc...
Submitted by Tim Hollobon on Wed, 09/08/2010 - 09:44
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.
Reply
Popular Articles
Last viewed:
- C# Download File with Progress Bar
- Install Windws 2003 Terminal Service Licenses (Remote Desktop)
- SQL Create Table Add Description to Column
- Windows Server 2003 Configure RRAS (Routing and Remote Access Service) Site to Site VPN
- SQL Server Log File (AKA LDF file) is huge - reduce size
- Using Nullable Data Types with C#

Recent comments
1 day 18 hours ago
6 days 8 hours ago
6 days 9 hours ago
1 week 18 hours ago
1 week 2 days ago
2 weeks 8 hours ago
2 weeks 1 day ago
2 weeks 4 days ago
2 weeks 6 days ago
2 weeks 6 days ago