Today a customer of SciChart asked us how to clear memory in SciChart. They were experiencing a memory leak, after adding 30,000,000 points to a SciChartSurface then removing the chart from its parent Grid, the memory usage in Task Manager just wouldn’t go down.
We take Memory leaks very seriously so devoted some time to diagnosing the problem. We found something in the WPF framework which really shocked us. Take a look at the following video, we think you’ll be surprised too!
Wow, so x:Name MarkupExtension in WPF actually keeps a reference to any UserControl or element named in XAML, even if it is removed from the Visual Tree. We’ve been coding WPF since the Cider Extensions to Visual Studio 2005, and never knew about this!
That’s not to say SciChart is perfect in this regard. We found another issue where if you clear the RenderableSeries after removing a surface, the XValues are kept in memory by the Axis Interactivity Helper.For a surefire way to ensure memory is freed, you can always clear the DataSeries like this:
// Ensure all DataSeries are cleared
So, if you get a memory leak in SciChart and clearing the DataSeries does not work, it’s worth running it against a profiler and seeing what the GC Roots are. If you see a tree like the above it could be because of the x:Name Markup Extension!