views:

169

answers:

6

I have code that looks like this:

/// <summary>
/// Dispose of the instance
/// </summary>
public void Dispose()
{
    if (_instance != null)
    {
        _instance = null;
        // 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);
    }
}

Although there is a comment that explains purpose of that GC-related call, I still don't understand why it's there.

Isn't object destined for garbage collection once all instances cease from existence (like, when used in using() block)?

What's the use case scenario where this would play important role?

Thanks!

+2  A: 

If your type implements a finalizer (~MyType() { }), it keeps the garbage collector from running it. Used when your finalizer takes care of unmanaged types, but the user has already called Dispose() (either explicitly or via a using() { } block), freeing those unmanaged types.

Jesse C. Slicer
0xA3's answer is a bit better as you **can** also deal with managed types in that scenario too...
Jesse C. Slicer
@Jesse: There's nothing to stop you *trying* to deal with managed types in your finaliser, but it's not really a good idea. Finalisation order isn't deterministic, so it's possible that child objects might have already been cleaned-up before their parent etc.
LukeH
+6  A: 

When implementing the dispose pattern you might also add a finalizer to your class that calls Dispose(). This is to make sure that Dispose() always gets called, even if a client forgets to call it.

To prevent the dispose method from running twice (in case the object already has been disposed) you add GC.SuppressFinalize(this);. The documentation provides a sample:

class MyResource : IDisposable
{
    [...]

    // This destructor will run only if the Dispose method 
    // does not get called.
    ~MyResource()      
    {
        // Do not re-create Dispose clean-up code here.
        // Calling Dispose(false) is optimal in terms of
        // readability and maintainability.
        Dispose(false);
    }

    // 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);
    }

    private 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.
            resource.Cleanup()          
        }
        disposed = true;         
    }
}
0xA3
You *might* add a finalizer. Very rarely though, in my experience. There are plenty of times where it makes sense to implement IDisposable without having a finalizer.
Jon Skeet
@Jon Skeet: Isn't adding a finalizer the way to make sure that `Dispose()` gets always called? How would unmanaged memory or other native resources be cleaned up then if it is not sure that `Dispose` is always called?
0xA3
Okay, but judging from sample, how are managed resources freed, in case that Dispose() is not called by user code (or by using block)? GC frees them automatically when finalizer is run?
mr.b
@mr. b: In my sample, the finalizer would only take care of unmanaged resources. All managed resources would be released by the garbage collector at some point in time.
0xA3
@mr.b: You only need a finalizer if you *directly* own unmanaged resources. If you've just got references to other implementations of IDisposable, you don't need a finalizer. `SafeHandle` makes finalizers almost always unnecessary, as that adds automatic finalization (of various flavours) for handles.
Jon Skeet
A: 

From MSDN: GC.SuppressFinalize:

This method sets a bit in the object header, which the system checks when calling finalizers. The obj parameter is required to be the caller of this method.

Objects that implement the IDisposable interface can call this method from the IDisposable..::.Dispose method to prevent the garbage collector from calling Object..::.Finalize on an object that does not require it.

Typically you would use this if your object does not reference other objects, just discrete types, or has already reset any object references to NULL.

Craig Trader
+3  A: 

Objects which can be finalized survive the first GC run.

Normally, when the GC detects that an object is unreachable, then it reclaims it. If the object is finalizable, then the GC does not reclaim it; instead, it considers it reachable nonetheless (and all the objects that this object references, and so on), and schedules it for finalization. The object will be reclaimed only when it is find again to be unreachable at some point after it has been finalized.

This means that a finalizable object incurs an extra cost: the object must be kept around in memory for a longer time. Hence the call you see: it is worthwhile to suppress finalization when it is not needed. Here, the object uses finalization to ensure that it is always "disposed of" at some point. When it is disposed explicitly, it needs not be finalized anymore.

Thomas Pornin
I've never heard of this particular behaviour - is there a reference for it, something in the MSDN docs perhaps?
Aaronaught
@Aaronaught: The "Finalization Internals" section of this article is a good overview: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
LukeH
+1  A: 

You shoud take a look at this other post :)

http://stackoverflow.com/questions/151051/when-should-i-use-gc-suppressfinalize

Juan Nunez
A: 

Garbage collection: GC reclaims the memory used by the object when the object is referenced no more.

Dispose: a method from the IDisposable interface that is to release all managed and unmanaged resources when the programmer calls it (either directly or indirectly via a using block).

Finalizer: a method to release all unmanaged resources. Called by the GC before reclaiming the memory.

Managed resource: any .NET class that implements the IDisposable interface, like Streams and DbConnections.

Unmanaged resource: the stuffing wrapped in the Managed Resource classes. Windows handles are the most trivial examples.


Now to answer your question:

GC keeps a list (Finalization Queue) of all objects the class of which declares a Finalizer (~ClassName in C#). Objects are put in this queue upon creation. GC runs periodically to check whether there are any objects inaccessible from the program. It then checks if any of the inaccessible objects are referenced from the Finalization Queue, and puts these in another queue called the Freacheable queue, while the rest are reclaimed. A separate thread is used to run the Finalize methods of objects in the Freacheable queue.

The next time GC runs, it will find that some of the objects previously in the Freacheable queue are already Finalized thus are ready for reclaiming. Note that GC needs at least two cycles (or a lot more if there is a lot of Finalization to do) to get rid of an object with a Finalizer, which incurs some performance penalties.

The SuppressFinalize method simply sets a flag in the object header which indicates that the Finalizer does not have to be run. This way GC can reclaim the memory of the object right away. As per the definition above, the Dispose method does the same thing as the Finalizer (and more), so if it is executed then Finalization is not neccessary any more. Using the SuppressFinalize method, you can save some work for the GC by notifying it about this fact. Additionally, now you don't have to implement checks in the Finalizer to avoid double releasing. The only problem with Dispose is that is is not guaranteed to run, because it is the programmer's responsibility to call it, that is why sometimes we need to bother with Finalizers.


That being said, it is only very very rarely that you need to write a Finalizer, because for the vast majority of the usual unmanaged resources a managed wrapper already exists, and managed resources are to be released by calling their Dispose methods from your own Dispose method, and from there only! In finalizers you must never call a Dispose method.


Further reading:

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

http://www.codeproject.com/KB/dotnet/idisposable.aspx

kicsit