'ObservableCollection causing a lot of GC Pressure
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
