开发者

Silverlight DataTemplate Memory Leak

Anyone have an explantion to me why the Style below applied to a Silverlight ListBox bound to a collection of 2000 Items leaks memory like crazy while scrolling trough the list? Memory usage开发者_如何学C goes into the hundreds of megabytes very quickly.

The leak only occurs if I leave the ItemsControl in. Otherwise memory consumption stays put. The leak also does not occur if the "Tags" property (Type: string[]) that the problematic ItemsControl is bound to returns a static readonly array from its getter.

This is the implementation of the "Tags" property getter and I have tried of calling the getters of a list items in a loop would also a leak but it doesn't. We are talking about 1-5 Tags per Collection Item = 10000 Strings at maximum. It appears as if the leak only occurs if the ItemsControl is bound to a non-static collection.

Runtime version is 4.0.60310.0 which AFAIK is higher than the one that's supposed to fix the DataTemplate MemoryLeak.

<Style x:Key="DocumentHybridListBox" TargetType="ListBox">
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Padding" Value="3"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel VirtualizingStackPanel.VirtualizationMode="Recycling"  Orientation="Vertical" Margin="1, 3, 1, 3" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Style="{StaticResource DocumentList_Hybrid_Image}">
                        <Image.Source>
                            <BitmapImage Behaviors:BindableBitmapImageSource.Source="{Binding PreviewPicture}"></BitmapImage>
                        </Image.Source>
                    </Image>
                    <Grid Grid.Column="1" Margin="6, 0, 0, 0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Text="{Binding Summary}" FontWeight="Bold" TextTrimming="WordEllipsis"></TextBlock>
                        <TextBlock Grid.Row="2" Text="{Binding DateSummary}" FontSize="11" TextTrimming="WordEllipsis"></TextBlock>

                        <!--- Problem starts here -->
                        <ItemsControl Grid.Row="1" ItemsSource="{Binding Tags}" Margin="0, 3, 0, 3">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"></StackPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Button Content="{Binding}" Style="{StaticResource TagButton}"
                                        Command="{Binding DataContext.TagDrillDownCmd, ElementName=Self}"
                                        CommandParameter="{Binding}"
                                        ToolTipService.ToolTip="{Binding Converter={StaticResource tagTooltipConverter}}"></Button>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

The Tags property:

private string[] _tags = null;
static readonly string[] constTags = new [] { "foo", "bar "};

public string[] Tags
{
    get
    {
        //return constTags; // this won't leak if bound to ItemsControl
        if (_tags == null)
        {
            // initialize
            if (Document.Tags != null && Document.Tags.Length != 0)
            {
                // initialize
                _tags = Document.Tags.Select(t => Decrypt2String(t,
                    ServiceLocator.Get<IKeyContainer>().DerivedContentEncryptionKey)).ToArray();
            }
        }

        return _tags;
    }

    set
    {
        _tags = value;
        OnPropertyChanged(()=>Tags);
    }
}


There is a known issue in Silverlight data templates related memory leake. Its been around for quite some time. Check this link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜