观察到造成大量GC压力
我在WPF应用程序中使用LiveCharts2,以显示实时图(100个数据点/s),
图表已连接到ObservableCollection。 我发现我的添加数据点的代码造成了很多GC压力:
要添加数据的代码如下:
// amount of datapoints to keep in chart per series
// (200 points / 25points per second = 8 seconds)
static int dataLength = 200;
// 4 series in total
private static List<ObservableCollection<ObservableValue>> seriesDataPoints
= new List<ObservableCollection<ObservableValue>>();
private static List<string> seriesNames = new List<string>();
// variable in attempt to reduce GC pressure
private static ObservableValue ReUseShell = null;
// add new datapoint (and remove old datapoint if series > dataLength )
public static void AddDataPoint(double point, string seriesName)
{
// search for the correct series
int seriesIndex = -1;
for (int i = 0; i < seriesNames.Count; i++)
{
if (seriesNames[i] == seriesName)
{
seriesIndex = i;
break;
}
}
// series was not found, create new serries
if (seriesIndex == -1)
{
seriesIndex = CreateNewSeries(seriesName);
}
// check if we have a reusable datapoint (in order to reduce gc)
if (ReUseShell != null)
{
ReUseShell.Value = point;
seriesDataPoints[seriesIndex].Add(ReUseShell);
ReUseShell = null;
}
else
{ // we dont have a empty data point (series < datalengt / ~initialisation)
seriesDataPoints[seriesIndex].Add(new(point));
}
// remove oldest datapoint and reuse it for the next data fill
if (seriesDataPoints[seriesIndex].Count > dataLength)
{
ReUseShell = seriesDataPoints[seriesIndex][0];
seriesDataPoints[seriesIndex].RemoveAt(0);
}
}
我的代码 /可观察收藏集有任何问题,还是该问题位于LiveCharts2库中?
I am using Livecharts2 in my WPF Application in order to show a realtime graph (100 data points/s)
The chart is connected to an ObservableCollection.
I found that my Code to add DataPoints is causing a lot of GC Pressure:
The Code to add Data is as follows:
// amount of datapoints to keep in chart per series
// (200 points / 25points per second = 8 seconds)
static int dataLength = 200;
// 4 series in total
private static List<ObservableCollection<ObservableValue>> seriesDataPoints
= new List<ObservableCollection<ObservableValue>>();
private static List<string> seriesNames = new List<string>();
// variable in attempt to reduce GC pressure
private static ObservableValue ReUseShell = null;
// add new datapoint (and remove old datapoint if series > dataLength )
public static void AddDataPoint(double point, string seriesName)
{
// search for the correct series
int seriesIndex = -1;
for (int i = 0; i < seriesNames.Count; i++)
{
if (seriesNames[i] == seriesName)
{
seriesIndex = i;
break;
}
}
// series was not found, create new serries
if (seriesIndex == -1)
{
seriesIndex = CreateNewSeries(seriesName);
}
// check if we have a reusable datapoint (in order to reduce gc)
if (ReUseShell != null)
{
ReUseShell.Value = point;
seriesDataPoints[seriesIndex].Add(ReUseShell);
ReUseShell = null;
}
else
{ // we dont have a empty data point (series < datalengt / ~initialisation)
seriesDataPoints[seriesIndex].Add(new(point));
}
// remove oldest datapoint and reuse it for the next data fill
if (seriesDataPoints[seriesIndex].Count > dataLength)
{
ReUseShell = seriesDataPoints[seriesIndex][0];
seriesDataPoints[seriesIndex].RemoveAt(0);
}
}
and this is how the chart looks:
Is there any issue with my code / Observable collection or does the issue reside with the Library LiveCharts2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论