Since .NET has a garbage collector why do we need finalizers/destructors/dispose-pattern?

前端 未结 12 1563
粉色の甜心
粉色の甜心 2020-12-07 09:09

If I understand correctly the .net runtime will always clean up after me. So if I create new objects and I stop referencing them in my code, the runtime will clean up those

相关标签:
12条回答
  • 2020-12-07 10:02

    Finalizers are needed to guarantee the release of scarce resources back into the system like file handles, sockets, kernel objects, etc. Since the finalizer always runs at the end of the objects life, it’s the designated place to release those handles.

    The Dispose pattern is used to provide deterministic destruction of resources. Since the .net runtime garbage collector is non-deterministic (which means you can never be sure when the runtime will collect old objects and call their finalizer), a method was needed to ensure the deterministic release of system resources. Therefore, when you implement the Dispose pattern properly you provide deterministic release of the resources and in cases where the consumer is careless and does not dispose the object, the finalizer will clean up the object.

    A simple example of why Dispose is needed might be a quick and dirty log method:

    public void Log(string line)
    {
        var sw = new StreamWriter(File.Open(
            "LogFile.log", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
    
        sw.WriteLine(line);
    
        // Since we don't close the stream the FileStream finalizer will do that for 
        // us but we don't know when that will be and until then the file is locked.
    }
    

    In the above example, the file will remain locked until the garbage collector calls the finalizer on the StreamWriter object. This presents a problem since, in the meantime, the method might be called again to write a log, but this time it will fail because the file is still locked.

    The correct way is to dispose the object when are done using it:

    public void Log(string line)
    {
        using (var sw = new StreamWriter(File.Open(
            "LogFile.log", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))) {
    
            sw.WriteLine(line);
        }
    
        // Since we use the using block (which conveniently calls Dispose() for us)
        // the file well be closed at this point.
    }
    

    BTW, technically finalizers and destructors mean the same thing; I do prefer to call c# destructors 'finalizers' since otherwise they tend to confuse people with C++ destructors, which unlike C#, are deterministic.

    0 讨论(0)
  • 2020-12-07 10:03

    Because the the Garbage Collector cannot collect what the managed environment did not allocate. Therefore any call to an unmanaged API that results in a memory allocation needs to be collected in the old fashioned way.

    0 讨论(0)
  • 2020-12-07 10:05

    The Garbage Collector will only run if the system is not under memory pressure, unless it really needs to free up some memory. That means, you can never be sure when the GC will run.

    Now, Imagine you are a Database Connection. If you let the GC clean up after you, you may be connected to the database for much longer than needed, causing weird load situation. In that case, you want to implement IDisposable, so that the user can call Dispose() or use using() to really make sure that the connection is closed ASAP without having to rely on GC which may run much later.

    Generally, IDisposable is implemented on any class that works with unmanaged resources.

    0 讨论(0)
  • 2020-12-07 10:09

    Objects that need descructors and dispose methods is using un-managed resources. So the garbage collector cannot clean up those resources, and you have to do this on your own.

    Look at the MSDN documentation for IDisposable; http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

    The example uses an un-managed handler - IntPr.

    0 讨论(0)
  • 2020-12-07 10:10

    The .NET garbage collector knows how to handle managed objects within the .NET runtime. But the Dispose pattern (IDisposable) is used primarily for un-managed objects that an application is using.

    In other words, the .NET runtime doesn't necessarily know how to deal with every type of device or handle out there (closing network connections, file handles, graphics devices, etc), so using IDisposable provides a way to say "let me implement some cleanup of my own" in a type. Seeing that implementation, the garbage collector can call Dispose() and ensure that things outside of the managed heap are cleaned up.

    0 讨论(0)
  • 2020-12-07 10:11

    The real reason is because .net garbage collection is NOT designed to collect unmanaged resources, therefore the cleanup of these resources still lies in the hands of the developer. Also, object finalizers are not automatically called when the object goes out of scope. They are called by the GC at some undetermined time. And when they get called, GC doesn't run it right away, it waits for the next round to call it, increasing the time to clean up even more, not a good thing when your objects are holding scarce unmanaged resources (such as files or network connections). Enter the disposable pattern, where the developer can manually release scarce resources at a determined time (when calling yourobject.Dispose() or the using(...) statement). Keep in mind you should call GC.SuppressFinalize(this); in your dispose method to tell the GC that the object was manually disposed and shouldn't be finalized. I suggest you take a look at the Framework Design Guidelines book by K. Cwalina and B. Abrams. It explains the Disposable pattern very good.

    Good Luck!

    0 讨论(0)
提交回复
热议问题