2012-04-27 46 views
3

所以我一直在考慮爲我寫的類實現一個析構函數,而且我不確定如何真正釋放內存,或者如果這將由垃圾回收處理。正確使用析構函數c#

class AutomatedTest 
{ 
    public bool testComplete = false; 
    public bool testStopRequest = false; 

    public List<Command> commandList = new List<Command>(); 

    private bool loggingEnabled = false; 
    ... 


    public AutomatedTest(TestList testToCreate) 
    { 
     // Create a list of Command objects and add them to the list 
    } 
} 

如何使用類:

for(int numTests = 0; numTests < 20; numTests++) 
{ 
    AutomatedTest test1 = new AutomatedTest(TestList._1DayTest); 
    RunAutoTest(test1); 

    AutomatedTest test2 = new AutomatedTest(TestList._2DayTest); 
    RunAutoTest(test2); 

    AutomatedTest test3 = new AutomatedTest(TestList._3DayTest); 
    RunAutoTest(test3); 

    AutomatedTest test4 = new AutomatedTest(TestList._4DayTest); 
    RunAutoTest(test4); 
} 

因此要創建和運行4個對象,這是做20次。
我的問題是我應該如何妥善處置/破壞這些對象?我不想假設這些垃圾已被收集,但我對實現解析器不熟悉。

+0

可能重複的優選[析構VS IDisposable的?](http://stackoverflow.com/questions/456213/destructor-vs-idisposable) – BrokenGlass 2012-04-27 19:30:25

回答

1

只要您不使用任何實現IDisposable的對象,您就不必手動處理或破壞。

0

最好的選擇是IDisposable模式詳細here。但正如其他人指出的那樣,只有當您的對象擁有一些昂貴的資源時纔有必要,比如文件句柄,流,DB對象等。其他一切只會由GC收集。相信它。

1

您將無法控制這些對象何時被垃圾收集。由於提到Henk Holterman,您可能需要考慮實施IDisposable,或使用IDisposable模式。如果你不需要做這個,我就不會在完成後用它來讓.NET知道擔心使用.Dispose()或終結,~AutomatedTest()

假設這些測試方法可能需要一段時間,你可以說test1 = null;這個對象引用不再被使用,否則一旦它超出範圍,GC就會清理乾淨。

0

我相信這是一個公平的規則是「如果你的任何對象處理非託管資源,或實現IDisposable,你需要處置它們。」我沒有看到你發佈的代碼中沒有任何管理,所以你可能不需要擔心。但是,顯然有一些我們看不到的代碼,所以我不能確定。

請閱讀此MSDN Article以瞭解正確處理的說明。

0

通常稱爲「析構函數」或Finalizer,直到對象被垃圾收集時纔會被調用。

所以如果你有資源需要釋放,最好實施IDisposable

0

處理處理昂貴對象的方法之一是實現IDisposable接口並實現Dispose和Finalize方法。當你的類依賴非託管代碼並負責清理它時,這是推薦的模式。更多詳細信息here

0

當你的對象被垃圾收集時,類的析構函數被調用。在像c#這樣的託管編程語言中,您無法控制垃圾回收器何時運行並執行析構函數。垃圾收集由CLR(公共語言運行時間)在看到該對象不再被引用或稍後在程序中使用時處理。在你的榜樣,考慮到代碼

AutomatedTest test1 = new AutomatedTest(TestList._1DayTest); 
RunAutoTest(test1); 

執行RunAutoTest(TEST1)後,「TEST1」引用變量不再使用,將可用於垃圾收集。然而,實際的垃圾收集過程可能無法立即運行,您無法確保它在特定時間運行。如果在你的AutomatedTest類中,你正在使用諸如打開FileStream等資源,那麼一旦你完成了使用該類的對象,就需要釋放這些資源。這可以通過你的類實現以下方式IDisposable接口來完成

class AutomatedTest:IDisposable 
    { 
     public void Dispose() 
     { 
      //free up any resources 
     } 
    } 

一旦你的類實現了IDisposable,你可以通過包裝使用它,它是建立一個「使用」塊

for (int numTests = 0; numTests < 20; numTests++) 
     { 
      using (AutomatedTest test1 = new AutomatedTest(TestList._1DayTest)) 
      { 
       RunAutoTest(test1); 
      } 
      //as soon as code exits the 'using' block the dispose method on test1 would be called 
      //this is something you cannot guarantee when implementing a destructor 

      using (AutomatedTest test2 = new AutomatedTest(TestList._2DayTest)) 
      { 
       RunAutoTest(test2); 
      } 
      //dispose on test2 will be called here 

      ///rest of code 
     } 

內僅供參考,可以使用〜在C#中實現析構函數。實施IDisposable接口的方法是在創建析構函數的

class AutomatedTest 
    { 

     ~AutomatedTest() 
     { 
      //code will run only on garbage collection 
     } 
    }