【问题标题】:Massive memory leak when binding ItemsControl to an Observable Collection将 ItemsControl 绑定到可观察集合时出现大量内存泄漏
【发布时间】:2010-02-21 19:57:09
【问题描述】:

我在 ScrollViewer 中有一个 ItemsControl。 ItemsControl.ItemSource 设置为 Observable Collection。

每次我向集合中添加一个项目时,我的应用程序的内存使用量几乎翻了一番,并且最终在添加了足够多的内容之后。

这是一个粗略的草图:

<Scrollviewer Name="MyScroll">
  <ItemsControl Name="MyItemsControl">

        .....standard itemscontrol code here, using <StackPanel> as presenter (VirtualizingStackPanel did not change my problem, btw)
       .........

       ..DataTemplate has three textboxes and a textblock

  </ItemsControl>

Code:

Class MyScroll

   Dim myOBSCol as ObservableCollection(StudyUnit) 'studyunit is the core object of my application
                             'just holds a series of text properties as  Dependency Properties

   Sub New()
      'create studyunit objects
      For x as integer = 0 to 50
         Dim SU as new StudyUnit
         '.then I Set studyunit properties (i.e. Su.Text = "...", Su.IDNum = "2", etc...)
         OBSCol.add(SU)
      Next

    MyItemsControl.ItemsSource=myOBSCol
   End Sub

End Class

(请原谅我无法重现我的确切代码。我的代码编译正常)

使用 ANTS Memory Profiler,我可以看到我的应用程序中的所有类实例。当我启动程序时,我有 150 个 TextBox 实例(数据模板中有 3 个)。当我将一个学习单元添加到集合中时,我有 303。添加另一个会留下 456... 609... 等等。

studyunit 对象继承自依赖对象,我只绑定到它的依赖属性。由于这篇文章:http://support.microsoft.com/kb/938416,我为 studyunit 做了一系列的依赖属性,但是并没有解决任何问题。

废话!


2010 年 2 月 22 日更新。这是实际代码(上面的代码非常简化,是从内存中创建的)

Private Sub ObservableCollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
    If e.Action = NotifyCollectionChangedAction.Remove Then 'q removed
        Dim oldDict As IList = e.OldItems
        If Not TryCast(oldDict(0), studyUnit) Is Nothing Then
            Try

            Catch ex As Exception

            End Try
        Else

        End If

    ElseIf e.Action = NotifyCollectionChangedAction.Add Then   'q added
        'FIND ITEM IN LIST WITH NEW ITEM ID
        Dim newQ As studyUnit
        newQ = e.NewItems(0)

        'set the location to provide focus after the new question is added
        focusIndex = _ObsCol.getObjPosition(newQ)
        Console.WriteLine("fi" + focusIndex.ToString)
    ElseIf e.Action = NotifyCollectionChangedAction.Reset Then 'list "reset"
        'call function that gives focus to the new control. (didn't affect memory issue when commented out'
            giveFocus(focusIndex)



    End If

'创建一个新的 StudyUnit 并添加到 ObservableCollection 的代码

Private Sub addNewQuestion(ByVal location As eInsertQuestion, ByRef sender As TextBox) '业务规则是在特殊的情况下按 Enter 键创建新问题 '“新问题”文本框,您希望在其中显示新问题

 Dim sentText As TextBox = sender


    'get qid of sender from "tag" object of the sender textbox
    Dim senderQID As String = CInt(sentText.Tag)

    'find  this 'sender' question in the existing observable collection
    Dim senderQuestion As studyUnit
    For Each su As studyUnit In _ObsCol
        If su.QID = senderQID Then
            senderQuestion = su
            Exit For
        End If
    Next


    Dim newQuestionSortOrder As Integer
    If location = eInsertQuestion.Before Then
        newQuestionSortOrder = CInt(senderQuestion.sortOrder)  'will take over the sortorder of the previous question
    ElseIf location = eInsertQuestion.After Then
        'insert new question before
        newQuestionSortOrder = CInt(senderQuestion.sortOrder) + 1
    End If


    'create the new question
    Dim newQ As New studyUnit
    'new "sort order"
    newQ.sortOrder = CStr(newQuestionSortOrder)
    'new "displayed order"
    newQ.displayedOrder = generateNewQuestionDisplayedOrder(senderQuestion.displayedOrder) 'create a random question # for the new quesiton
    'set HID to the sender's HID
    newQ.HID = CStr(senderQuestion.HID)
    'set type to "Question"  //possibly not needed
    'newQ.Add("suType", eSUnitType.Question)





    'now send this studyunit to the database (then we can get its QID in the database)
    My.Application.dComm.insertQuestion(newQ)
    'set "NEW Q" = the exact data inserted in the database (as a best practice)
    newQ = Nothing
    newQ = My.Application.dComm.getNewestQuestionStudyUnit()


    'AddHandler newQ.studyUnitChangedEvent, AddressOf StudyUnitAltered


    'add to main question collection...
    'find position of sender question
    Dim senderIndex As Integer = Me._ObsCol.getObjPosition(senderQuestion)
    Dim newLocation As Integer = senderIndex + location  '("location" will be equal to +1 (after) or 0 (before)

    'insert before or after that one
    If newLocation < _ObsCol.Count Then
        _ObsCol.Insert(newLocation, newQ)
    Else
        _ObsCol.Add(newQ)  'can't "insert" if index is greater than the current size of the collection, use add function instead
    End If


    For x As Integer = newLocation + 1 To _ObsCol.Count - 1 'obscol is zero-based
        Dim thisQ As studyUnit = CType(_ObsCol(x), studyUnit)

        If thisQ.suType = eSUnitType.Question Then

            'increase each question's sort order by 1
            thisQ.sortOrder = CStr(CInt(thisQ.sortOrder) + 1)
        Else
            'else: do nothing, this study unit is a heading or section, does not get a change in sortOrder
        End If
    Next

'下面是问题的一个很好的演示 '我试图重置 itemsource,但 DataTemplate 实例不会从内存中清除......即使第三行被注释。 items 控件将变为空,但所有数据模板对象仍保留在内存中。

    Me.SP_ItemsControl.ItemsSource = Nothing
    Me.SP_ItemsControl.Items.Clear()
    Me.SP_ItemsControl.ItemsSource = _ObsCol


End Sub

'下面是我的数据模板。这些由 DataTemplateSelector 选择:

<local:BindableRTBConverter x:Key="RTBConverter" />
<local:ColorConverter x:Key="myColorConverter"/>
<local:HeadingsNameConverter x:Key="myHeadingConverter"/>


<!-- Styles that can be used throughout all three datatemplates here-->

<Style x:Key="borderStyleSettings" TargetType="Border">
    <Setter Property="Border.Margin" Value="5,5,5,5"/>
    <Setter Property="Border.BorderBrush" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=borderColor, Converter={StaticResource myColorConverter}}" />
    <Setter Property="Border.BorderThickness" Value=".9"/>
    <Setter Property="Border.CornerRadius" Value="6"/>
</Style>


<Style x:Key="textStyleSettings" TargetType="TextBlock">
    <Setter Property="TextBlock.FontSize" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontSize}" />
    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
</Style>

<!--end of styles-->


<!--Section RN Template-->
<DataTemplate x:Key="RNSectionTemplate">
    <DataTemplate.Resources>

    </DataTemplate.Resources>

        <Border Tag="{Binding Path=SID, Mode=OneTime}" Style="{StaticResource borderStyleSettings}">
        <StackPanel>
            <TextBlock Margin="3,3,3,0" Foreground="Black" FontStyle="Italic" FontWeight="Bold" HorizontalAlignment="Center" Style="{StaticResource textStyleSettings}">
                 <TextBlock.Text> 
                       <MultiBinding Converter="{StaticResource myHeadingConverter}" 
                                     ConverterParameter="getRNSectionTitle" Mode="OneWay">
                           <Binding Path="num"/>
                           <Binding Path="name"/>
                       </MultiBinding>         
                    </TextBlock.Text>
            </TextBlock>       
        </StackPanel>
        </Border>


</DataTemplate>

<!--Heading RN Template-->
<DataTemplate x:Key="RNHeadingTemplate">
    <DataTemplate.Resources>

    </DataTemplate.Resources>

    <Border Tag="{Binding Path=HID, Mode=OneTime}" Style="{StaticResource borderStyleSettings}">
        <StackPanel>
            <TextBlock Margin="3,3,3,0" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Left" Style="{StaticResource textStyleSettings}">
                 <TextBlock.Text> 
                       <MultiBinding Converter="{StaticResource myHeadingConverter}" 
                                     ConverterParameter="getRNHeadingTitle" Mode="OneWay">
                           <Binding Path="num"/>
                           <Binding Path="name"/>
                       </MultiBinding>         
                    </TextBlock.Text>
            </TextBlock>
        </StackPanel>
    </Border>
</DataTemplate>





<!--Question RN Template-->
<DataTemplate x:Key="RNQuestionTemplate">
    <DataTemplate.Resources>
        <Style TargetType="local:BindableRTB">
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontFamily" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontName}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <!--Sets changes container of textbox control from ScrollViewer to Adorner Decorator, as an attempt to
                        reduce the memory waste in "scrollbar" instances.  Didn't help much.  Also didn't impact my memory leak.-->
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border 
                                  Name="Border"
                                  CornerRadius="0" 
                                  Padding="0"
                                  Background="White"
                                  BorderThickness="0"
                         >
                            <AdornerDecorator Margin="0" x:Name="PART_ContentHost"></AdornerDecorator>

                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="TextBox">
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="BorderThickness" Value="3"/>
            <Setter Property="FontFamily" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontName}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border 
                                  Name="Border"
                                  CornerRadius="0" 
                                  Padding="0"
                                  Background="White"
                                  BorderThickness="0"
                         >
                            <AdornerDecorator Margin="0" x:Name="PART_ContentHost"></AdornerDecorator>

                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style TargetType="TextBlock">
            <Style.Setters>
                <Setter Property="Foreground" Value="Purple"></Setter>
                <Setter Property="FontFamily" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontName}"/>
            </Style.Setters>
        </Style>

    </DataTemplate.Resources>

    <!-- Main border -->
    <Border Style="{StaticResource borderStyleSettings}">
        <Grid Name="myStack" HorizontalAlignment="Stretch"  Margin="4" Tag="{Binding Path=QID, Mode=OneTime}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>

            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
               <!-- last column for controls to be added later, if wanted-->
            </Grid.ColumnDefinitions>


      <!--Row 0-->

            <!-- Displayed Order textbox (editable) -->
                <TextBox Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="1" IsTabStop="False" BorderThickness="0">
                    <TextBox.Text>
                        <Binding Path="displayedOrder"  Mode="TwoWay"  UpdateSourceTrigger="LostFocus"/>
                    </TextBox.Text>
                </TextBox>

            <!-- delete button -->
            <Ellipse  Grid.Column="2" Grid.Row="0" Tag="{Binding Path=QID, Mode=OneTime}" Name="ellipseDelete" Height="12" Width="12"  Stroke="Black" 
                      Fill="LightGray" Stretch="Fill" HorizontalAlignment="Right"></Ellipse>


      <!-- Row 1 -->

            <!-- Main text area -->
            <local:BindableRTB Name="myRTB" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
                <local:BindableRTB.Document>
                    <Binding Converter="{StaticResource RTBConverter}" Path="Question" Mode="TwoWay" UpdateSourceTrigger="LostFocus"/>
                </local:BindableRTB.Document>
            </local:BindableRTB>

            <!--Page Ref-->

            <TextBox Name="txtPageRef" Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="1" IsTabStop="False">
                <TextBox.Text>
                    <Binding Path="pageRef"  Mode="TwoWay"  UpdateSourceTrigger="LostFocus"/>
                </TextBox.Text>
            </TextBox>

      <!-- Row 2 -->


            <!-- New question textbox -->
            <StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Stretch">
                <StackPanel.Style>
                    <Style TargetType="StackPanel">
                        <Style.Setters>
                            <Setter Property="Background" Value="Transparent" />
                        </Style.Setters>
                        <Style.Triggers>
                            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                <Setter Property="Background" Value="LightGreen"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>

                <!-- Binding the questions "QID" to the 'new quesiton' textbox.  For the bubbling  keydown event,
                This information can help determine where to insert the new question, and then give focus
                to that new question-->
                <TextBox Name="newQuestionTextBox" Tag="{Binding Path=QID, Mode=OneTime}" Background="Transparent" IsReadOnly="True" BorderThickness="0" FontSize="10" HorizontalAlignment="Left">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <Trigger Property="IsKeyboardFocused" Value="True">
                                    <Setter Property="FontStyle" Value="Italic"/>
                                    <Setter Property="Text" Value="(Start typing to create a new question. Press the ALT key to insert a new question above.)"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </StackPanel>

            <!-- Endof New question textbox -->

        </Grid>
    </Border>

</DataTemplate>
<!--End of reviewnote Templates-->

数据模板选择器

导入 System.Windows.Controls 导入 System.Windows 公共类类型DataTemplateSelector 继承 DataTemplateSelector

Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As System.Windows.DependencyObject) As System.Windows.DataTemplate

    Dim sUnit As studyUnit = DirectCast(item, studyUnit)

    Dim mainWindow As R2_CoreWindow = CType(My.Application.MainWindow, R2_CoreWindow)

    If sUnit.suType = eSUnitType.Heading Then
        Return mainWindow.WpfEditor.FindResource("RNHeadingTemplate")
    ElseIf sUnit.suType = eSUnitType.Section Then

        Return mainWindow.WpfEditor.FindResource("RNSectionTemplate")

    ElseIf sUnit.suType = eSUnitType.Question Then
        Return mainWindow.WpfEditor.FindResource("RNQuestionTemplate")
    End If
End Function

结束类

'ItemsControl XAML

<!-- Scrollviewer_Keydown is looking for bubbling keydown event from New Quesitons-->

<ScrollViewer.Resources>
    <!-- Template selector for each Data Template -->
    <local:typeDataTemplateSelector x:Key="myTempSelector"></local:typeDataTemplateSelector>
</ScrollViewer.Resources>

<ItemsControl Name="SP_ItemsControl" ItemTemplateSelector="{StaticResource myTempSelector}">
    <!--Set the itemssource in code later-->
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ItemsPresenter/>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>

            <StackPanel></StackPanel>

            <!-- Use of virtualizingstackpanel didn't help -->
            <!--<VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />-->

        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

</ItemsControl>


StudyUnit(浓缩...对于 15 个以上的差异属性重复同样的事情)


公开课学习单元 继承依赖对象

Public Property Question() As String
    Get
        Return GetValue(QuestionProperty)
    End Get
    Set(ByVal value As String)
        SetValue(QuestionProperty, value)
    End Set
End Property
Public Shared QuestionProperty As DependencyProperty = DependencyProperty.Register("Question", GetType(String), _
    GetType(DependencyObject), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnQuestionPropertyChanged)))
Private Shared Sub OnQuestionPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)

'目前不使用此回调...将代码放在这里与我的数据库对话,这是我的内容的原始来源 结束子 '所有其他属性的设置与上面的完全一样

【问题讨论】:

  • 问题出在您的StudyUnit 课程中,而不是您发布的代码中。
  • 这可能是什么问题?它是一个包含 17 个不同依赖属性的依赖对象。每个依赖属性都有一个 ref。共享属性ChangdeCallback(即“OnDocumentPropertyChanged”)...没什么异常...?
  • 我想我们可能需要在这里查看更多代码。从数字来看,当您更新集合时,您不仅添加了单个新项目,还重新添加了所有现有项目。要么是那个,要么是你的模板中的某些东西导致了问题;但是到目前为止,将 ItemsControl 绑定到 ObservableCollection 是一个经过良好测试的场景,所以如果任何正常的绑定都可能导致这种情况,我会感到惊讶。但恐怕很难在没有看到更多内容的情况下提供详细信息。
  • 对于像 StudyUnit 这样的业务对象,通常建议将其实现为非 DependencyObject 并实现 INotifyPropertyChanged(您链接的知识库文章中的选项 2)。 DependencyObjects 一般多用于需要绑定targets的WPF对象;绑定 sources 不需要它。
  • 好的,我更新了,希望额外的代码有用。感谢您的帮助!

标签: .net wpf vb.net memory-leaks itemscontrol


【解决方案1】:

一切都会好起来的!

问题在于将我的可观察集合绑定到我的 ItemsControl 的 ItemsSource 属性。

无论出于何种原因(也许有人可以解释这一点),每次我将一个项目添加到可观察集合时,ItemsControl 都必须重新创建列表中的所有项目。这导致了非常不规则的内存问题(有时会增加一倍,缓慢增加,甚至随着添加的每个项目而减少)。

由于我的要求,我能够接受手动管理 Items 集合的不太优雅的解决方案。 (我能够毫无问题地保持每个项目与 studyunit 对象的绑定,这是我的应用程序的核心需求。

要强制更改 ObservableCollection 以更新 UI,我只需要添加此代码(我只显示了添加单个项目所需的代码):

 Private Sub obscolchanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)


    If e.Action = NotifyCollectionChangedAction.Add Then   'q added
        Dim newSU As studyUnit

        newSU = e.NewItems(0)

        dim newIndex as Integer = _ObsCol.IndexOf(newSU)

        If newIndex < Me.ItemsControl.Items.Count
           Me.ItemsControl.Items.Insert(newIndex, newSU)
        Else
           'when newSU is the last item, simply add to the end of the collection, or you will get an IndexOutOfRange exception
           Me.ItemsControl.Items.Add(newSU)
        End If

     End If
End Sub

可以编写类似的代码来处理移动事件和删除事件。这显然不如使用 ItemsSource 属性方便,但我没有遇到内存问题,并且添加项目的速度明显更快,因为当我更改 Collection 时 ItemsControl 不必重新生成每个 DataTemplate。

感谢 ItemsControl,在我研究 30 分钟的情况下,您花费了将近 40 个小时 :)

【讨论】:

    【解决方案2】:

    问题出在框架上,将 itemsource 更改为 datagrid 或 datagridview,一切正常且更快

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2011-02-16
      • 2011-12-03
      相关资源
      最近更新 更多