Memory Leak when using dynamic Tabs

Dec 19, 2016 at 2:50 PM

I my application I am using a button, which when pressed, several Tabs are created and each tab has an enclosing D3 ChartPlotter. So every time I press the button I delete the previous Tab Items and create the new ones.

The way I was closing the tabs was just by using the
However after a more in-depth research I have found out that the ChartPlotter leave something behind that prevets the Garbage Collector from releasing the memory. So every time I press the button, the old tabs disappear and the new appear BUT the memory of the old tabs is Still in Use.

To help you understand better I will give you a simplified code of how I create and populate the individual tabs inside a for loop of varying size (some time it could iterate 10 times or 5 or 15 or... ) :
for( Varying Number Of Itterations)
// Create Anindividual Tab 
                TabItem IndividualResultsTab = new TabItem();
// Create the individual ChartPlotter
                ChartPlotter chartPlotter = new ChartPlotter();                
                HorizontalDateTimeAxis MyAxis2 = new HorizontalDateTimeAxis();
                chartPlotter.HorizontalAxis = MyAxis2;
// Create the X and Y data
                DateTime[] X_ArrayM = new DateTime[somedates];
                double[] Y_ArrayM = new double[someValues];
                EnumerableDataSource<DateTime> datesDataSourceM = new EnumerableDataSource<DateTime>(X_ArrayM);
                datesDataSourceM.SetXMapping(x => MyAxis.ConvertToDouble(x));
                datesDataSourceM.AddMapping(ShapeElementPointMarker.ToolTipTextProperty, (DateTime X) => string.Format("Date is: {0}", X);
// Create Tooltips      
                CompositeDataSource compositeDataSourceM = datesDataSourceM.Join(Y_ArrayM.AsYDataSource());
                IPointDataSource pointDataSourceM = compositeDataSourceM;
// Add Line Chart
                this.chart = Plotter2DExtensions.AddLineGraph(chartPlotter, pointDataSourceM, new Pen(Brushes.Green, 2), circleElementPointMarker, new PenDescription("NTI " + MasterNTINum.ToString()));

When I run the VS2015 memory diagnostic tool I get that the most memory increase in each ittereation is in the following Object Types:
  1. "RenderData" of the PresentationCore.dll
  2. "Pen" of the PresentationCore.dll
  3. "Ellipse" of the PresentationFramework.dll
  4. "ToolTip" of the PresentationFramework.dll
I believe that there might be some events that I need to detach before I call the tabControl.Items.Clear(); but to be honest I have no idea which events.

I am not using any events myself, but there must be some MouseOver events, because I can hover the mouse over the plots and get the data.

The problem is I have no idea how to precede as I am still quite new to C# in general so I cannot quite understand event handling yet.

Could you please suggest any ways that I can overcome this memory leak?

Should the Reply of Battula32 in the following post answer my problem?
Link to another answer

Thank you.

PS: I cannot use static Tabs, because the number of tabs each time could by varying. So I need to create Tabs inside a for loop, however I am not sure that the way I am doing it is the correct way.