2016-09-16 38 views
0

我在我的程序中使用Nevron圖表。下面的代碼是一個點擊按鈕事件:創建新實例使用大量內存

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

每次點擊程序分配大量內存後,甚至GC.Collect()不乾淨的內存,如果我每次都使用Nevron圖的一個實例,並清理數據然後添加新的數據,每件事情都可以。

有什麼問題?

更新1:這裏是功能

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

您應該在分配新實例之前處置現有的nChartControl1 –

+0

「每次清理數據」時,您能否詳細說明這意味着什麼? –

+0

@ LasseV.Karlsen我更新了整個功能 – mojtaba357

回答

-1

始終釋放內存分配新的內存塊之前。

因此:在創建新的NChartControl實例之前,處理當前分配給this.nChartControl的內存。垃圾收集器不完美,完全依靠垃圾收集器是不好的做法。

+0

如果在這種情況下處理圖表控件的行爲有所幫助,那麼這不是因爲垃圾收集器不完美,這是因爲仍然在某處引用圖表控件,並且處置它會刪除這些引用。 –

1

我只是測試使用如下代碼控制:

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(); 
      } 
     } 
    } 

了。在控制沒有內存泄漏。所以答案是你需要調用dispose或者使用using語句。

+0

在我的項目中不起作用,拋出內存不足。 – mojtaba357