绑定到ObservableCollection导致内存泄漏

时间:2015-04-02 21:52:29

标签: c# wpf memory-leaks observablecollection inotifypropertychanged

我将ObservableCollection绑定到AreaSeries。 OC经常更新,每次更新时都会占用更多内存,而不会对旧数据进行垃圾回收,因此它会不断增长。

这是我的XAML:

<charting:AreaSeries Name="areaSeries" ItemsSource="{Binding Path=Data}"  HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" DependentValueBinding="{Binding Path=Y}" IndependentValueBinding="{Binding Path=X}" TransitionDuration="00:00:00">
    <charting:AreaSeries.DataPointStyle>
        <Style TargetType="charting:AreaDataPoint">
            <Setter Property="Width" Value="0"/>
            <Setter Property="Height" Value="0"/>
            <Setter Property="Background" Value="DarkBlue"/>
        </Style>
    </charting:AreaSeries.DataPointStyle>
</charting:AreaSeries>

Data是类型为PointViewModel的ObservableCollection:

public ObservableCollection<PointViewModel> Data
{
    get { return m_data; }
}

PointViewModel实施ViewModelBase

public class PointViewModel : ViewModelBase

ViewModelBase实施INotifyPropertyChanged, IDisposable

以下是Data的更新方式:

int xPos = 0;
foreach (float fv in m_fHistogramValues)
{
    if (m_data.Count - 1 < xPos)
        m_data.Add(new PointViewModel(m_fHistogramXValues[xPos], fv, false));
    else
    {
        m_data[xPos].X = m_fHistogramXValues[xPos];
        m_data[xPos].Y = fv;
    }

    xPos++;
}

我在网上找到的所有东西都说要确保实现IPropertyNotified以避免内存泄漏,但我认为它是正确实现的。我错过了什么吗?

0 个答案:

没有答案