This project is read-only.

Memory leak in D3

May 1, 2013 at 6:47 PM
Hello!

I have the same problem like here: https://dynamicdatadisplay.codeplex.com/discussions/356125

In few words, i'm using simple method, where i every second remove all LineGraphs from ChartPlotter and add new LineGraph.

I use this to add line:
GraphPlotter.AddLineGraph(pointsList, new Pen(Brushes.Black, 2), new CircleElementPointMarker {Size = 5.0, Brush = Brushes.Blue, Fill = Brushes.Blue}, new PenDescription("description"));

and this to remove:
GraphPlotter.Children.RemoveAll(typeof(LineGraph));
GraphPlotter.Children.RemoveAll(typeof(ElementMarkerPointsGraph));

I control the number of childrens and it is 16 always when i enter method again, so this is not problem with "not removed children".

Eventually after each cycle of remove/add LineGraph, the memory, which my app is using, is increasing by 1-2 mb.

Hope for your help.
May 6, 2013 at 7:07 AM
Hi

Somehow the linegraph what you are adding is different from line what you are trying to delete.
LineGraph line = plotter.AddLineGraph(......) has return type.
LineAndMarker<MarkerPointGraph> lineAndMarker = plotter.AddLineGraph(...)
Please create a collection of LineGraph and LineAndMarker<...> and remove these collected elements from the children
 /// <summary>
    /// Chart plotter data class used in chartplotter to manipulate with plot name(entity name) and plot id(entity identifier)
    /// </summary>
    public class ChartPlotterKey
    {
        /// <summary>
        /// Gets or sets the name of the plot.
        /// </summary>
        /// <value>The name of the plot.</value>
        public string PlotName
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the plot id.
        /// </summary>
        /// <value>The plot id.</value>
        public Guid PlotId
        {
            get;
            set;
        }
    }

******************** Add marker and line
 LineAndMarker<MarkerPointsGraph> lineAndMarker = injectedPlotter.AddLineGraph(pointDataSource, new Pen(brush, 2), new CirclePointMarker { Size = 9, Fill = brush }, new PenDescription("pen Name"));
                lineAndMarker.LineGraph.Tag = "aaaa";
                chartPlotterKey.PlotName = (string)lineAndMarker.LineGraph.Tag;
                this.lineAndMarkerGraphs.Add(chartPlotterKey, lineAndMarker);

************************* Add line graph
 LineGraph lineGraph = injectedPlotter.AddLineGraph(pointDataSource, color, 1, "bbbbb");
                lineGraph.Tag ="bbbbb";
                chartPlotterKey.PlotName = (string)lineGraph.Tag;
                this.lineGraphLines.Add(chartPlotterKey, lineGraph);
*********************

****** Remove lines
 foreach (KeyValuePair<ChartPlotterKey, LineGraph> line in this.lineGraphLines)
            {
                line.Value.DataChanged -= new EventHandler(this.LineGraph_DataChanged);
            }

            this.lineGraphLines.Clear();

            foreach (KeyValuePair<ChartPlotterKey, LineAndMarker<MarkerPointsGraph>> line in this.lineAndMarkerGraphs)
            {
                line.Value.LineGraph.DataChanged -= new EventHandler(this.LineGraph_DataChanged);
            }

            this.lineAndMarkerGraphs.Clear();

            foreach (KeyValuePair<Guid, InjectedPlotter> kvp in this.injectedPlotters)
            {
                kvp.Value.Viewport.PropertyChanged -= this.OnInjectedPlotterViewportPropertyChanged;
                kvp.Value.MainVerticalAxis.MouseDown -= this.OnInjectedVerticalAxisMouseDown;

                kvp.Value.Children.Clear();

                if (this.Children.Contains(kvp.Value))
                {
                    this.Children.Remove(kvp.Value);
                }
            }

            this.injectedPlotters.Clear(); 
try something like this. Or if you have the sample where this code doesn't work, please share it, I will try to find the issue.

Regards
Ravi
Sep 24, 2013 at 12:20 PM
Same here! memory leak!
Jun 3, 2016 at 9:14 PM
Well, it might be a bit late for the OP but it seems that I experienced a similar issue with the WPF version.

After some digging I found that there is a binding that is not cleared correctly.

The LineLegendItem object creates a DataTemplate which binds to a PenDescription. This is used for displaying a LineGraphs' color and title in the legend.

To work around you can subscribe to the graph's removal from the plotter and clean it manually:
private void addSeries()
{
   ...
   LineGraph seriesOnGraph = plotter.AddLineGraph( ... );
   seriesOnGraph.PlotterDetaching += SeriesOnGraph_PlotterDetaching;
}

private void SeriesOnGraph_PlotterDetaching(object sender, PlotterConnectionEventArgs e)
{
   LineGraph graph = sender as LineGraph;
   if(graph != null)
   {
      graph.Description.LegendItem.Content = null;
   }
}
Don't forget to remove the graphs from the plotter as suggested by Ravi.
private void GraphControl_Unloaded(object sender, RoutedEventArgs e)
{
   plotter.RemoveUserElements();
}