-->

Silverlight DataTemplate Memory Leak

2020-08-01 05:08发布

问题:

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 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);
    }
}

回答1:

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