2012-04-27 63 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 
     } 
    }