本文介绍了Silverlight DataTemplate内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都对我有个偏见,为什么下面的样式应用于绑定到2000个项的集合的Silverlight列表框时,在滚动列表时会像疯了一样泄漏内存?内存使用量很快就达到了数百兆.

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.

仅当我将ItemsControl留在其中时才会发生泄漏.否则将保持内存消耗.如果有问题的ItemsControl绑定到的标签"属性(类型:string [])从其吸气剂返回静态只读数组,也不会发生泄漏.

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.

这是"Tags"属性getter的实现,我尝试过在循环中调用列表项的getter也会泄漏,但事实并非如此.我们正在谈论的是每个收集项目1-5个标签=最多10000个字符串.似乎只有在ItemsControl绑定到非静态集合时才会发生泄漏.

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.

运行时版本为4.0.60310.0,其AFAIK高于应修复DataTemplate MemoryLeak的版本.

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>

标签属性:

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

推荐答案

Silverlight数据模板中存在与内存泄漏有关的已知问题.它已经存在了一段时间.检查链接

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

这篇关于Silverlight DataTemplate内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:44