ItemsSource会造成内存泄漏

ItemsSource会造成内存泄漏

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

问题描述

我只是在后台创建一个Observable集合并将其绑定.

I'm simply creating a Observable collection in Background and binding to it.

所有对象都继承自INotifyPropertyChanged.但是尽管如此,内存消耗仍在不断增加.

All objects inherit from INotifyPropertyChanged.but nevertheless the Memory consumption is continuously raising.

以下Objects实例不断提高

The following Objects instances are continuously raising

WeakReference
FrugalObjectList<WeakEventManager+Listener>
ConditionalWeakTable<TKey, TValue>+Entry<Object, Object>[]
WeakEventTable+EventKey
ConditionalWeakTable<Object, Object>
SingleItemList<WeakEventManager+Listener>
Object
Int32[]
WeakEventManager+ListenerList<NotifyCollectionChangedEventArgs>
WeakEventManager+ListenerList<PropertyChangedEventArgs>
HybridDictionary
ListDictionary
ListDictionary+DictionaryNode
WeakEventManager+ListenerList<EventArgs>
WeakEventManager+ListenerList<CurrentChangingEventArgs>
CollectionRecord

我正在使用.net 4.5.2.

I'm using .net 4.5.2.

另请参见以下屏幕截图:

See also the following Screenshots:

MemoryConsumptionOverview

类增加

随附示例代码

XAML标记:

<Window x:Class="BindingDataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="120" Width="200"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <DataGrid ItemsSource="{Binding BindingVals, Mode=OneWay}" />
    </Grid>
</Window>

后面的代码:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Threading;

namespace BindingDataGridTest
{
    public partial class MainWindow : INotifyPropertyChanged
    {

        public ObservableCollection<Values> BindingVals { get; set; }

        public MainWindow()
        {
            BindingVals = new ObservableCollection<Values>();
            InitializeComponent();

            DispatcherTimer myTimer = new DispatcherTimer { Interval = new TimeSpan(20) };
            myTimer.Tick += CreateVals;
            myTimer.Start();

        }
        private void CreateVals(object sender, EventArgs e)
        {
            Values myMainval = new Values
            {
                MyVal = "1V" + new Random().Next()
            };

            BindingVals.Clear();
            BindingVals.Add(myMainval);

            GC.Collect();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Values : INotifyPropertyChanged
    {
        public string MyVal { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

    }

}

推荐答案

这是由DispatcherTimer引起的.

It's caused by DispatcherTimer.

只要将对象的方法绑定到计时器,DispatcherTimer就会使该对象保持活动状态. https://msdn .microsoft.com/ru-ru/library/system.windows.threading.dispatchertimer(v = vs.110).aspx

A DispatcherTimer will keep an object alive whenever the object's methods are bound to the timer.https://msdn.microsoft.com/ru-ru/library/system.windows.threading.dispatchertimer(v=vs.110).aspx

分别使用System.Timers.Timer和Dispatcher.

Use System.Timers.Timer instead and Dispatcher separately.

    public MainWindow()
    {
        BindingVals = new ObservableCollection<Values>();
        InitializeComponent();

        System.Timers.Timer myTimer = new Timer {Interval = 20};
        myTimer.Elapsed += CreateVals;
        myTimer.Start();

    }

    private void CreateVals(object sender, EventArgs e)
    {
        Dispatcher.Invoke(() =>
        {
            Values myMainval = new Values
            {
                MyVal = "1V" + new Random().Next()
            };

            BindingVals.Clear();
            BindingVals.Add(myMainval);

            GC.Collect();
        });

    }

这篇关于绑定WPF DataGrid ItemsSource会造成内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:00