title: Proper Disposal of SciChartSurface
body: <p>I'm currently using a similar structure to the <a href="https://www.scichart.com/example/wpf-chart-example-50-channel-eeg/" rel="nofollow noreferrer">50 EEG example</a>. Part of my design is to add and delete 10-50 function plots on the fly, so it is important that I managed my memory effectively. When I run the memory profiler in VS2015, I find that everytime I try to a function plot from the collection, SciChart.Charting.Visuals.Axes.LabelProviders.NumericTickLabel still lingers in memory. What would be the proper way to dispose NumericTickLabel or remove a scichartsurface?</p>
  
  <p>XAML.CS</p>
  
  <pre><code>    &lt;ResourceDictionary&gt;
         &lt;DataTemplate x:Key="FunctionItemTemplate"&gt;
         &lt;Grid&gt;
             &lt;s:SciChartSurface Name=FunctionSurface
                                RenderableSeries="{Binding RenderableSeriesCollections}"/&gt;
  
         &lt;/Grid&gt;
      &lt;/ResourceDictionary&gt;
  
      &lt;ListView Name="FunctionPlots
            ItemTemplate="{StaticResource FunctionItemTemplate}"
            ItemsSource="{Binding FunctionsCollection}" /&gt;
  </code></pre>
  
  <p>MainWindowViewModel.cs</p>
  
  <pre><code>public class MainWindowViewModel : INotifyProperty
  {
         private ObservableCollection&lt;FunctionPlotViewModel&gt; _functionsCollection;
  
         public MainWindowViewModel()
         {
             FunctionsCollection = new ObservableCollection&lt;FunctionPlotViewModel&gt;();
         }
  
         public ObservableCollection&lt;FunctionPlotViewModel&gt; FunctionsCollection
         {
               get { return _functionsCollection;}
               set 
               {
                     _functionsCollection = value;
                     OnPropertyChanged("FunctionsCollection");     
               }  
         }
  
         public void RemoveLastFunctionPlot()
         {
                int lastIndex = FunctionCollections.Count - 1;
                FunctionCollections.Remove(lastIndex);
                //Doesn't Collect
                GC.Collect();
                GC.WaitForPendingFinalizer();
                GC.Collect();
         }
  
         public event PropertyChangedEventHanlder PropertyChanged;
         protected void OnPropertyChanged(string name)
         {
                  PropertyChangedEventHanlder handler = PropertyChanged;
  
                  if (handler != null)
                  {
                      handler(this, new PropertyChangedEventArgs(name));
  
                  }
          }
  
  }
  </code></pre>
  
  <p>FunctionPlotViewModel.cs</p>
  
  <pre><code> public class FunctionPlotViewModel: INotifyProperty
      {
         private ObservableCollection&lt;IRenderableSeries&gt; _renderableSeriesCollections;
  
         public FunctionPlotViewModel()
         {
             RenderableSeriesCollection = new ObservableCollection&lt;IRenderableSeries&gt;();
         }
  
         public ObservableCollection&lt;IRenderableSeries&gt; RenderableSeriesCollections
         {
               get { return _renderableSeriesCollections;}
               set 
               {
                     _renderableSeriesCollections= value;
                     OnPropertyChanged("RenderableSeriesCollections");     
               }  
         }
  
         public  void SetUpCustomAxis(double TimeOfDayInitial, IAxis AxisToBindTo)
         {
               var defaultSurface = RenderableSeriesCollections.FirstOrDefault().DataSeries.ParentSurface();
                if(defaultSurface == null)
                {
                    return;
                }
                var newAxis = new NumericAxis();
                var customNumericLabel = new CustomNumericLabelProvider();
                defaultSurface.XAxis = newAxis;
                Binding visibleRangeBinding = new Binding("VisibleRange");
                visibleRangeBinding.Source = AxisToBindTo;
                visibleRangeBinding.Mode = BindingMode.TwoWay;
                ((NumericAxis)defaultSurface.XAxis).SetBinding(NumericAxis.VisibleRangeProperty, visibleRangeBinding);
  
         }
  
         public void CleanUp()
         {
             var defaultSurface = RenderableSeriesCollections.FirstOrDefault().DataSeries.ParentSurface();
                if(defaultSurface == null)
                {
                    return;
                }
             foreach(var renderSeries in RenderableSeriesCollections)
             {
                  renderSeries.DataSeries.Clear();
             }
             RenderableSeriesCollections.Clear();
             var token  = defaultSurface.SuspendUpdates();
             token.Dispose();
  
             //More things to do??
         }
  
         public event PropertyChangedEventHanlder PropertyChanged;
         protected void OnPropertyChanged(string name)
         {
                  PropertyChangedEventHanlder handler = PropertyChanged;
  
                  if (handler != null)
                  {
                      handler(this, new PropertyChangedEventArgs(name));
  
                  }
          }
  
  }
  </code></pre>
  
  <p>CustomNumericLabelProvider.cs</p>
  
  <pre><code>     public class CustomNumericLabelProvider : NumericProvider
       {
         public override void OnBeginAxisDraw()
         {
         }
  
         public override string FormatLabel(IComparable dataValue)
         {
              if (dataValue == null)
                  return String.Emtpy;
              return ConvertToTimeOfDay(dataValue);
  
         }
  
         public string ConvertToTimeOfDay(IComparable value)
         {
               //commented out for brevity
               return value;
         }
  
         public override string FormatCursorLabel(IComparable dataValue)
         {
              //commented out for brevity
              return dataValue;
         }
  
      }
  </code></pre>