Большая утечка памяти при привязке ItemsControl к наблюдаемой коллекции - PullRequest
2 голосов
/ 21 февраля 2010

У меня есть ItemsControl в ScrollViewer.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Профилировщик, я могу видеть все экземпляры классов в моем приложении.Когда я запускаю программу, у меня есть 150 экземпляров TextBox (их три на табличке данных).Когда я добавляю учебное подразделение в коллекцию, у меня 303. Добавление другого оставляет мне 456 ... 609 ... и т. Д.

Объекты учебного модуля наследуются от объектов зависимости, и я привязываюсь только к его свойствам зависимости,Из-за этой статьи я сделал из учебного блока ряд свойств зависимостей: http://support.microsoft.com/kb/938416,, но это ничего не исправило.

Черт!


Обновление 2/22 /2010.Вот фактический код (приведенный выше код был очень упрощен и создан из памяти)

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 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

' НИЖЕ ОТЛИЧНОДЕМОНСТРАЦИЯ ПРОБЛЕМЫ 'Я пытаюсь сбросить источник данных, но экземпляры DataTemplate не удаляются из памяти ... даже если третья строка закомментирована.Элемент управления станет пустым, но все объекты таблицы данных останутся в памяти.

    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 Открытый класс typeDataTemplateSelectorНаследует 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+ свойств diff)


Public Class studyUnit Наследует DependencyObject

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)

', не использующий этот обратный вызов в данный момент ... Поместиткод здесь, чтобы поговорить с моей базой данных, которая является исходным источником для моего контента End Sub ', все остальные свойства установлены ТОЧНО, как указано выше

Ответы [ 2 ]

2 голосов
/ 23 февраля 2010

все лучше!

Проблема связана с привязкой моей наблюдаемой коллекции к свойству ItemsSource моего ItemsControl.

По какой-либо причине (и, возможно, кто-то может объяснить это), каждый раз, когда я добавляю элемент в наблюдаемую коллекцию, ItemsControl должен заново создавать все элементы в моем списке. Это приводило к очень нерегулярным проблемам с памятью (иногда удваивалось, медленно увеличивалось или даже уменьшалось с каждым добавленным элементом).

Из-за моих требований я смог принять менее изящное решение ручного управления коллекцией Предметов. (Я могу сохранить привязку каждого элемента к studyunit объекту без проблем, что было основной потребностью моего приложения.

Чтобы принудительно внести изменения в ObservableCollection для обновления пользовательского интерфейса, мне просто нужно было добавить этот код (я только показал код, необходимый для ДОБАВЛЕНИЯ одного элемента):

 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, но у меня не возникало проблем с памятью, и добавление элементов происходило значительно быстрее, поскольку ItemsControl не нужно заново генерировать каждый DataTemplate при изменении коллекции.

И спасибо, ItemsControl, за то, что я потратил почти 40 часов, когда я выделил 30 минут:)

0 голосов
/ 09 апреля 2018

проблема в платформе, измените источник данных на datagrid или datagridview, и все будет работать нормально и быстрее

...