IDisposable:内存泄漏

Arn*_* F. 4 c# memory-leaks idisposable

我目前正在审查一个C#应用程序,我在其中看到:

public static bool ServiceExists(string servicename)
{
    ServiceController[] services = ServiceController.GetServices();
    foreach (ServiceController s in services)
    {
        if (s.ServiceName == servicename)
        {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在这个答案中,Henk表示不使用Dispose()(或using)不会产生内存泄漏,无论是真是假?

我可以保留以前的代码吗或者我应该写一些像:

public static bool ServiceExists(string servicename)
{
    ServiceController[] services = ServiceController.GetServices();
    bool exists = false;

    foreach (ServiceController s in services)
    {
        using(s)
        {
            if (s.ServiceName == servicename)
            {
                exists = true;
            }
        }
    }

    return exists;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,不使用Dispose()(或using)有什么风险?

Jar*_*Par 6

如果Dispose没有调用方法,正确编写的对象不应该导致内存泄漏.现在,控制非托管资源的.Net对象应该通过SafeHandle实例来实现.这些将确保即使Dispose未被调用也释放本机内存.

但是,如果Dispose没有被调用,那么未正确写入的对象很可能会产生内存泄漏.我见过很多这样的对象的例子.

一般IDisposable情况下,如果您使用的是您拥有的实例,则应始终致电Dispose.即使对象写得正确,也可以提前清理非托管资源,而不是以后清理.

编辑

正如James在评论中指出的那样,有一种情况是不调用Dispose可能导致内存泄漏.一些对象使用Dispose回调来解除长期存在的事件,如果它们保持连接会导致对象驻留在内存中并构成泄漏.永远打电话的另一个原因Dispose