写一个IDisposable的实际例子

时间:2017-05-18 00:48:12

标签: c# .net memory-leaks idisposable

我一直在阅读有关Dispose模式的内容,我有点理解它的用途(清理资源以便我的应用程序赢得内存泄漏)但我还是希望看到它是一个实际的例子。

我的想法是编写一个简单的应用程序,首先使用一些资源并且不处理它们,并且在一些代码更改之后,正确地处理这些资源。我希望看到的是代码更改之前/之后的内存使用情况,以显示处理方式的帮助。

问题:我可以使用哪些对象?我尝试用一​​些大图像(JPEG图像大小超过15 MB)来做这件事,但我不能用它来构建一个实际的例子。当然,我对其他想法持开放态度。

2 个答案:

答案 0 :(得分:2)

此单元测试执行您所描述的内容。无论是否调用Dispose都可以在不调用Dispose的情况下显示所发生的事情。

两种方法都创建一个文件,打开文件,写入文件,然后再次打开并再次写入文件。

第一种方法会抛出IOException,因为StreamWriter没有被处理掉。该文件已经打开,无法再次打开。

第二个在尝试重新打开文件之前处理,并且没有例外。

[TestClass]
public class DisposableTests
{
    [TestMethod]
    [ExpectedException(typeof(IOException))]
    public void DoesntDisposeStreamWriter()
    {
        var filename = CreateFile();
        var fs = new StreamWriter(filename);
        fs.WriteLine("World");
        var fs2 = new StreamWriter(filename);
        fs2.WriteLine("Doesn't work - the file is already opened.");
    }

    [TestMethod]
    public void DisposesStreamWriter()
    {
        var filename = CreateFile();
        var fs = new StreamWriter(filename);
        fs.WriteLine("World");
        fs.Dispose();
        var fs2 = new StreamWriter(filename);
        fs2.WriteLine("This works");
        fs2.Dispose();
    }

    private string CreateFile()
    {
        var filename = Guid.NewGuid() + ".txt";
        using (var fs = new StreamWriter(filename))
        {
            fs.WriteLine("Hello");
        }
        return filename;
    }
}

您可能无法看到内存使用问题,因为这并不是IDisposable地址的具体内容。所有物体都使用记忆,但大多数都不是一次性的。垃圾收集器从不再引用的对象中回收内存(如在方法中创建的对象,但在方法结束时超出范围。)

IDisposable适用于类保留一些不被垃圾收集的资源的情况。另一个例子是SqlConnection。它不是关于内存,而是关于与SQL Server的连接。只有这么多。 (它最终会被释放,但不是以可预测的方式 - 这是一个题外话。)

类可能IDisposable的确切原因各不相同。他们往往没有任何共同之处。 IDisposable并不关心原因。这只是意味着课堂上需要清理的东西。

答案 1 :(得分:1)

以下示例显示了实现IDisposable接口的一般最佳实践。创建一些MyResouce实例并在不调用Dispose方法的情况下对其进行测试,存在内存泄漏。完成MyResouce实例后,每次都调用Dispose,没有内存泄漏。 Reference

public class DisposeExample
{
    // A base class that implements IDisposable. 
    // By implementing IDisposable, you are announcing that 
    // instances of this type allocate scarce resources. 
    public class MyResource: IDisposable
    {
        // Pointer to an external unmanaged resource. 
        private IntPtr handle;
        // Other managed resource this class uses. 
        private Component component = new Component();
        // Track whether Dispose has been called. 
        private bool disposed = false;

        // The class constructor. 
        public MyResource(IntPtr handle)
        {
            this.handle = handle;
        }

        // Implement IDisposable. 
        // Do not make this method virtual. 
        // A derived class should not be able to override this method. 
        public void Dispose()
        {
            Dispose(true);
            // This object will be cleaned up by the Dispose method. 
            // Therefore, you should call GC.SupressFinalize to 
            // take this object off the finalization queue 
            // and prevent finalization code for this object 
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scenarios. 
        // If disposing equals true, the method has been called directly 
        // or indirectly by a user's code. Managed and unmanaged resources 
        // can be disposed. 
        // If disposing equals false, the method has been called by the 
        // runtime from inside the finalizer and you should not reference 
        // other objects. Only unmanaged resources can be disposed. 
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called. 
            if(!this.disposed)
            {
                // If disposing equals true, dispose all managed 
                // and unmanaged resources. 
                if(disposing)
                {
                    // Dispose managed resources.
                    component.Dispose();
                }

                // Call the appropriate methods to clean up 
                // unmanaged resources here. 
                // If disposing is false, 
                // only the following code is executed.
                CloseHandle(handle);
                handle = IntPtr.Zero;

                // Note disposing has been done.
                disposed = true;

            }
        }

        // Use interop to call the method necessary 
        // to clean up the unmanaged resource.
        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);

        // Use C# destructor syntax for finalization code. 
        // This destructor will run only if the Dispose method 
        // does not get called. 
        // It gives your base class the opportunity to finalize. 
        // Do not provide destructors in types derived from this class.
        ~MyResource()
        {
            // Do not re-create Dispose clean-up code here. 
            // Calling Dispose(false) is optimal in terms of 
            // readability and maintainability.
            Dispose(false);
        }
    }
    public static void Main()
    {
        // Insert code here to create 
        // and use the MyResource object.
    }
}