When to dispose and why?

后端 未结 12 1037
臣服心动
臣服心动 2020-12-10 14:20

I asked a question about this method:

// Save an object out to the disk
public static void SerializeObject(this T toSerialize, String filename)
{
           


        
相关标签:
12条回答
  • 2020-12-10 14:35

    You are correct that for properly written code the GC will eventually clean up the native resources. The object will have a finalizer, and during finalization will free up the necessary native resources.

    However when this happens is very non-deterministic. Additionally it's a bit backwards because you're using the GC which designed to handle managed memory as a means to manage native resources. This leads to interesting cases and can cause native resources to stay alive much longer than anticipated leading to situations where

    • Files are open long after they are no longer used
    • Resource handles can run out because the GC doesn't see enough memory pressure to force a collection and hence run finalizers

    The using / dispose pattern adds determinism to the cleanup of native resources and removes these problems.

    0 讨论(0)
  • 2020-12-10 14:38

    If you know you're not going to use a certain resource you can simply dispose of it yourself; you will certainly be faster than the garbage collector and will allow others to use the file or whatever you have opened faster. The easiest way would be to use your TextWriter or any other resource in a using:

    using (TextWriter textWriter = new StreamWriter(filename))
    {
        xmlSerializer.Serialize(textWriter, toSerialize);
    }
    

    This basically ensures the TextWriter is disposed at the end. You don't need it any more than that, anyway.

    0 讨论(0)
  • 2020-12-10 14:38

    If you are opening a resource (such as a file, or opening a database connection) then disposing the resource will release its hold on the resource. If you don't do this then other people might not be able to connect to the database or use the file.

    As a general rule of thumb....if the class implements the IDisposable interface, then you should call the Dispose() method when you are finishing it. More than likely there was a reason for them making it disposable :)

    0 讨论(0)
  • 2020-12-10 14:46

    The rule of thumb here is pretty simple: always call Dispose() on objects that implement IDisposable (not all objects do). You won't always know the reason why an object had to implement Dispose, but you should assume that it is there for a reason.

    The easiest way to make sure you do this is through using:

    using (TextWriter tw = new StreamWriter(fileName))
    {
       // your code here
    }
    

    This will call Dispose() automatically at the end of the using block (it's fundamentally the same as using a try/catch/finally with the Dispose() in the finally block).

    For more information on how Dispose works with garbage collection, see here.

    0 讨论(0)
  • 2020-12-10 14:48

    The reason to call Dispose rather than waiting for the GC is often because you are using a finite system resource which you want to release as quickly as possible so that other processes or threads can use it. For objects that have the IDisposable pattern, the "using" construct is an easy and readable way to make sure that Dispose is called.

    0 讨论(0)
  • 2020-12-10 14:49

    Garbage collector releases all resources, but the time when it does this is undefined. Dispose method provides a way to release unmanaged resources immediately.

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