2016-09-16 2 views
0

J'utilise Nevron Chart dans mon programme. le code suivant est un événement de bouton de clic:Créer une nouvelle instance utilise beaucoup de mémoire

this.nChartControl1 = new NChartControl(); 
// add data to chart ... 

après chaque clic programme alloue beaucoup de mémoire et même GC.Collect() ne nettoie pas la mémoire, et si j'utilise une instance de Nevron graphique et chaque fois que nettoyer les données puis ajouter de nouvelles données tout est OK.

quel est le problème?

Update 1: ici est la fonction

private void button_Click(object sender, RoutedEventArgs e) 
    { 
     //if (nChartControl1 == null) 
     { 
      this.nChartControl1 = new NChartControl(); 
     } 

     // clear data 
     nChartControl1.Charts.Clear(); 
     nChartControl1.Panels.Clear(); 
     GC.Collect(); 

     // empty the grid then add NevronChart 
     this.grid.Children.Clear(); 
     this.grid.Children.Add(nChartControl1); 

     nChartControl1.BackgroundStyle.FrameStyle.Visible = false; 

     // set a chart title 
     NLabel title = nChartControl1.Labels.AddHeader("2D Line Chart"); 

     // setup chart 
     NCartesianChart chart = new NCartesianChart(); 
     nChartControl1.Panels.Add(chart); 
     chart.DockMode = PanelDockMode.Fill; 
     chart.Margins = new NMarginsL(2, 0, 2, 2); 
     chart.Projection.SetPredefinedProjection(PredefinedProjection.Orthogonal); 
     chart.LightModel.EnableLighting = false; 
     chart.Axis(StandardAxis.Depth).Visible = false; 
     chart.Wall(ChartWallType.Floor).Visible = false; 
     chart.Wall(ChartWallType.Left).Visible = false; 
     chart.BoundsMode = BoundsMode.Stretch; 
     chart.Height = 40; 
     chart.RangeSelections.Add(new NRangeSelection()); 
     chart.Axis(StandardAxis.PrimaryX).ScrollBar.Visible = true; 
     chart.Axis(StandardAxis.PrimaryY).ScrollBar.Visible = true; 

     // setup the line series 
     NLineSeries line = (NLineSeries)chart.Series.Add(SeriesType.Line); 
     //line.Values.FillRandom(new Random(), 10); 
     SetRandomData(line); 

     line.DataLabelStyle.Visible = false; 
     line.Legend.Mode = SeriesLegendMode.DataPoints; 
     line.ShadowStyle.Type = ShadowType.GaussianBlur; 
     line.ShadowStyle.Offset = new NPointL(new NLength(3, NGraphicsUnit.Pixel), new NLength(3, NGraphicsUnit.Pixel)); 
     line.ShadowStyle.FadeLength = new NLength(5, NGraphicsUnit.Pixel); 
     line.ShadowStyle.Color = System.Drawing.Color.FromArgb(55, 0, 0, 0); 
     line.LineSegmentShape = LineSegmentShape.Line; 


     nChartControl1.Controller.Tools.Add(new NSelectorTool()); 
     nChartControl1.Controller.Tools.Add(new NAxisScrollTool()); 
     nChartControl1.Controller.Tools.Add(new NDataZoomTool()); 
     NDataPanTool dpt = new NDataPanTool(); 
     nChartControl1.Controller.Tools.Add(dpt); 
     nChartControl1.Legends.Clear(); 
     nChartControl1.Refresh(); 
    } 
+0

Vous devez disposer du nChartControl1 existant avant de lui affecter une nouvelle instance –

+0

"chaque fois que vous nettoyez les données", pouvez-vous préciser ce que cela signifie exactement? –

+0

@ LasseV.Karlsen je mis à jour avec l'ensemble de la fonction – mojtaba357

Répondre

-1

mémoire Toujours deallocate avant d'attribuer un nouveau morceau de mémoire.

donc: disposez de la mémoire actuellement allouée à this.nChartControl avant de créer une nouvelle instance de NChartControl. S'appuyer entièrement sur son éboueur est une mauvaise pratique, car les éboueurs ne sont pas parfaits.

+0

Si le fait de disposer du contrôle de graphique dans ce cas aide, alors ce n'est pas parce que le garbage collector n'est pas parfait, c'est parce qu'il y a toujours des références au contrôle de diagramme quelque part, et l'éliminer supprime ces références. –

1

Je viens de tester le contrôle en utilisant le code suivant:

private void button1_Click(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 100000; i++) 
     { 
      using (NChartControl chartControl = new NChartControl()) 
      { 
       // set a chart title 
       NLabel title = chartControl.Labels.AddHeader("2D Line Chart"); 

       // setup chart 
       NCartesianChart chart = new NCartesianChart(); 
       chartControl.Panels.Add(chart); 
       chart.DockMode = PanelDockMode.Fill; 
       chart.Margins = new NMarginsL(2, 0, 2, 2); 
       chart.Projection.SetPredefinedProjection(PredefinedProjection.Orthogonal); 
       chart.LightModel.EnableLighting = false; 
       chart.Axis(StandardAxis.Depth).Visible = false; 
       chart.Wall(ChartWallType.Floor).Visible = false; 
       chart.Wall(ChartWallType.Left).Visible = false; 
       chart.BoundsMode = BoundsMode.Stretch; 
       chart.Height = 40; 
       chart.RangeSelections.Add(new NRangeSelection()); 
       chart.Axis(StandardAxis.PrimaryX).ScrollBar.Visible = true; 
       chart.Axis(StandardAxis.PrimaryY).ScrollBar.Visible = true; 

       // setup the line series 
       NLineSeries line = (NLineSeries)chart.Series.Add(SeriesType.Line); 
       //line.Values.FillRandom(new Random(), 10); 

       line.DataLabelStyle.Visible = false; 
       line.Legend.Mode = SeriesLegendMode.DataPoints; 
       line.ShadowStyle.Type = ShadowType.GaussianBlur; 
       line.ShadowStyle.Offset = new NPointL(new NLength(3, NGraphicsUnit.Pixel), new NLength(3, NGraphicsUnit.Pixel)); 
       line.ShadowStyle.FadeLength = new NLength(5, NGraphicsUnit.Pixel); 
       line.ShadowStyle.Color = System.Drawing.Color.FromArgb(55, 0, 0, 0); 
       line.LineSegmentShape = LineSegmentShape.Line; 


       chartControl.Controller.Tools.Add(new NSelectorTool()); 
       chartControl.Controller.Tools.Add(new NAxisScrollTool()); 
       chartControl.Controller.Tools.Add(new NDataZoomTool()); 
       NDataPanTool dpt = new NDataPanTool(); 
       chartControl.Controller.Tools.Add(dpt); 
       chartControl.Legends.Clear(); 
       chartControl.Refresh(); 
      } 
     } 
    } 

Et il n'y avait pas de fuite de mémoire dans le contrôle. Donc, la réponse est que vous devez appeler disposer ou utiliser une déclaration d'utilisation.

+0

ne fonctionne pas dans mon projet, jette de la mémoire. – mojtaba357