如何最大限度地减少 WPF Toolkit 图表的内存使用?
我有以下情况。
有一张折线图,一次可以显示 10 条线。每行包含 355 个数据点。用户可以通过选择/取消选择包含数据系列的数据对象来打开和关闭每条线。
图表线如下所示:
<charting:LineSeries ItemsSource="{Binding DataSlot1, Mode=OneWay}"
IndependentValueBinding="{Binding X, Mode=OneWay}"
DependentValueBinding="{Binding Y, Mode=OneWay}"
DataPointStyle="{StaticResource styleForDataSlot1}"
IsSelectionEnabled="False" />
我将数据从 ViewModel 加载到每个 10 个“数据槽”中,如下所示。
数据点是结构:
public struct SimpleDataPoint
{
public double X { get; set; }
public double Y { get; set; }
}
ViewModel 包含“数据槽”:
private SimpleDataPoint[][] _dataSlots;
ViewModel 通过属性公开数据槽:
public SimpleDataPoint[] DataSlot1
{
get { return _dataSlots[0]; }
}
当我将数据加载到槽中时,我会执行以下操作:
_dataSlots[freeSlot] = myDataProvider.GetPoints();
从槽中清除数据我会执行以下操作:
_dataSlots[slotToClear] = null;
在每次添加/删除数据操作之后 的插槽
OnPropertyChanged("DataSlot1");
对于我称之为问题: 如果用户长时间添加和删除图表的数据行,我的应用程序的内存使用量很快就会达到约 100 MB。
为了确定问题是否不在我的代码中,我注释掉了 OnPropertyChanged 调用。内存没有上去,当然图表也没有显示任何数据。
所以我认为问题是图表没有释放内存。当我执行_dataSlots[slotToClear] = null并且图表上相应的数据线消失后,内存没有释放,并且在我完全关闭图表所在的WPF视图后,内存也没有恢复正常。
我什至尝试在每个 dataSlots[slotToClear] = null 之后调用 System.GC.Collect() 但它没有帮助。
如何强制 WPF Toolkit 图表释放不再显示的数据系列的内存?
I have a following situation.
There is one line chart which can display 10 lines at a time. Each line contain 355 data points. User can turn each of the line on and off by selecting/deselecting data objects which contain the data series.
Chart lines look like this:
<charting:LineSeries ItemsSource="{Binding DataSlot1, Mode=OneWay}"
IndependentValueBinding="{Binding X, Mode=OneWay}"
DependentValueBinding="{Binding Y, Mode=OneWay}"
DataPointStyle="{StaticResource styleForDataSlot1}"
IsSelectionEnabled="False" />
and I load the data into each of 10 "data slots" from ViewModel as follows.
Data points are structs:
public struct SimpleDataPoint
{
public double X { get; set; }
public double Y { get; set; }
}
ViewModel contains "data slots":
private SimpleDataPoint[][] _dataSlots;
and ViewModel exposes data slots through properties:
public SimpleDataPoint[] DataSlot1
{
get { return _dataSlots[0]; }
}
when I load data into the slot, I do something like this:
_dataSlots[freeSlot] = myDataProvider.GetPoints();
to clear data from the slot I do:
_dataSlots[slotToClear] = null;
After each add/remove data operation for a slot I call
OnPropertyChanged("DataSlot1");
The problem:
if the user works for a long time adding and removing data lines for the chart, the memory usage for my application soon goes up to some 100 megabytes.
To find if the problem is not in my code, I commented away OnPropertyChanged calls. The memory did not go up and of course the chart did not display any data.
So I assume the problem is that the chart is not freeing the memory. The memory is not freed after I do _dataSlots[slotToClear] = null and the corresponding data line on the chart disappears, and also the memory does not return to normal after I completely close the WPF view where the chart is.
I tried even calling System.GC.Collect() after each dataSlots[slotToClear] = null but it does not help.
How can I force the WPF Toolkit chart to free the memory for data series which are not displayed any more?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我的示例中,我有 1 个包含 365 个点的图表。该图表在其自己的用户控件
ChartView
中定义。主窗口如下所示:按钮的事件处理程序:
顺序如下:
ChartView
控件。ChartView
控件。启动后应用程序消耗26Mb内存。
第一次更新 - 32Mb。
第二次更新 - 35Mb。
但下次更新不会超过这个值,它是最大值。
3650 点(10 倍以上)的数字是:52 -> 68 -> 88.
我不知道如果使用10行会发生什么,但内存消耗必须低于100Mb。
In my example I have 1 chart with 365 points. The chart is defined in its own user control
ChartView
. The main window looks so:The event handler of the button:
The sequence is following:
ChartView
control from the visual tree.ChartView
control.After the start the application consumes 26Mb of memory.
First update - 32Mb.
Second update - 35Mb.
But next updates will not exceed this value, it is the maximum.
With 3650 point (10 times more) the numbers are: 52 -> 68 -> 88.
I don't know what will happens if to use 10 lines, but memory consumption must be any lower than 100Mb.