Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I dispose my objects in .NET?

Tags:

For general code, do I really need to dispose an object? Can I just ignore it for the most part or is it a good idea to always dispose an object when your 100% sure you don't need it anymore?

like image 209
danmine Avatar asked Oct 29 '08 05:10

danmine


People also ask

When should I use Dispose?

The Dispose Method—Explicit Resource Cleanup The Dispose method generally doesn't free managed memory—typically, it's used for early reclamation of only the unmanaged resources to which a class is holding references. In other words, this method can release the unmanaged resources in a deterministic fashion.

Why do we need Dispose in C#?

In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

When to use finalize vs Dispose?

Method dispose( ) is used to free unmanaged resources whenever it is invoked. Method finalize( ) is used to free unmanaged resources before the object is destroyed. The method dispose( ) is to be implemented whenever there is a close( ) method. The method finalize( ) is to be implemented for unmanaged resources.


2 Answers

Dispose of an object the instant your are done with it. Disposable objects represent objects holding a valuable resource which the CLR is not intrinsically aware of. Consequently the GC is also unaware of the resources and is unable to make intelligent decisions as to when it should collect a disposable object and hence free the underlying resource.

Eventually the GC will feel memory pressure and collect your object by coincidence (nothing more). If you don't dispose of objects in a deterministic manner then it is completely possible to enter a resource starved state with almost no memory pressure.

Quick example of how this can happen. Lets think of the underlying resource as Win32 handle. These are very finite and fairly small. You run an operation that create a lot of Foo objects. Foo objects implement IDisposable and are responsible for creating and disposing of a Win32 handle. They are not manually freed and by a difference quirk make it into the Gen2 heap. This heap is freed fairly infrequently. Over time enough Foo instances make it into the Gen2 heap to take up all of the available handles. New Foo objects are consequently unable to be created regardless of how much memory is being used.

In fact to free the handles, it would take a rather large amount of memory to be allocated during a single operation to give enough pressure to free the instances.

like image 69
JaredPar Avatar answered Sep 29 '22 18:09

JaredPar


If the object implements IDisposable, you should dispose of it as soon as you are done with it. The easiest way is to surround it with a using block:

using (SqlCommand cmd = new SqlCommand(conn)) {
    cmd.ExecuteNonQuery();
}
like image 28
John Sheehan Avatar answered Sep 29 '22 16:09

John Sheehan