2011-05-08 94 views
1

任何人都有一個解釋給我爲什麼下面的樣式應用於綁定到2000項目的集合的Silverlight列表框的項目泄漏內存像瘋了一邊滾動列表?內存使用量非常快速地達到數百兆字節。Silverlight DataTemplate內存泄漏

只有當我離開ItemsControl時纔會發生泄漏,否則內存消耗仍然存在。如果有問題的ItemsControl綁定的「Tags」屬性(Type:string [])從其getter返回靜態只讀數組,則不會發生泄漏。

這是「標籤」屬性getter的實現,我曾嘗試在循環中調用列表項的getters,但也不會泄漏。我們正在討論1-5個標籤每個收藏品項目=最多10000個字符串。看起來只有在ItemsControl綁定到非靜態集合時纔會發生泄漏。

運行時版本是4.0.60310.0其中AFAIK高於應該修復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); 
    } 
} 

回答

0

中有相關的記憶利克的Silverlight數據模板的已知問題。它已經有相當一段時間了。檢查this鏈接

+1

我知道那個線程。但至少DataTemplate相關的泄漏應該在前一個補丁中修復。 – 2011-05-08 16:52:30