2016-09-16 2 views
0

내 프로그램에서 Nevron 차트를 사용합니다. 다음 코드는 클릭 버튼 이벤트입니다 : 클릭 할 때마다 프로그램이 많은 메모리를 할당하고, 심지어 GC.Collect()가 메모리를 청소하지 않습니다 후새 인스턴스 만들기 많은 메모리를 사용합니다

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

, 나는 하나 개의 Nevron 차트의 인스턴스마다 데이터를 정리를 사용하는 경우 그리고 나서 모든 것이 OK 인 새로운 데이터를 추가하십시오.

무엇이 문제입니까?

업데이트 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

관련 문제