Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到ObservableCollection导致内存泄漏_C#_Wpf_Memory Leaks_Observablecollection_Inotifypropertychanged - Fatal编程技术网

C# 绑定到ObservableCollection导致内存泄漏

C# 绑定到ObservableCollection导致内存泄漏,c#,wpf,memory-leaks,observablecollection,inotifypropertychanged,C#,Wpf,Memory Leaks,Observablecollection,Inotifypropertychanged,我正在将一个可见集合绑定到一个区域序列。OC会频繁更新,每次更新都会占用更多内存,而不会对旧数据进行垃圾收集,因此它会不断增长 这是我的XAML: <charting:AreaSeries Name="areaSeries" ItemsSource="{Binding Path=Data}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" DependentValueBinding="{Bin

我正在将一个可见集合绑定到一个区域序列。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>
PointViewModel实现ViewModelBase:

并且ViewModelBase实现INotifyPropertyChanged,IDisposable

以下是数据更新的方式:

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以避免内存泄漏,但我认为它的实现是正确的。我错过什么了吗

你曾经清理过你的m_数据吗?正如我所见,您只添加到它。我尝试在foreach循环之前添加m_data.Clear,但没有任何区别。PointViewModels只应在第一次添加时添加,因为它将256个项目添加到OC中,后续每次都应仅替换X和Y属性。您的程序是否因OutOfMemoryException而崩溃,或者到底发生了什么?不,内存使用量只是在不断增长。如果我允许的话,它会从200MB变为5GB,大概如果它足够大,最终会崩溃,有相当多的RAM可用。
public class PointViewModel : ViewModelBase
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++;
}