观察到造成大量GC压力

发布于 2025-01-31 17:23:14 字数 2148 浏览 2 评论 0原文

我在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:
enter image description here

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:
enter image description here

Is there any issue with my code / Observable collection or does the issue reside with the Library LiveCharts2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文