Quantcast
Channel: eTechPlanet.com
Viewing all articles
Browse latest Browse all 10

Garbage Collection in .Net | Destructors Versus Dispose

$
0
0

.Net Framework ships with an inbuilt garbage collector that manages the allocation and release of memory for your application. When the garbage collector performs a collection, it checks for objects that are no longer being used by the application and performs the necessary operations to reclaim their memory.

But Garbage Collector is not deterministic, means that we can never be sure when the garbage collector will be invoked. Basically garbage collector consists of an optimizing engine which determines the best time to perform a collection, based upon the allocations being made. We can force the Garbage Collector to be called to perform a collection by calling System.GC.Collect() method but that's not recommended.

 

Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. (Managed heap is a segment of memory used by garbage collector to store and manage objects).
  • The System.GC.Collect() method is called.

We have destructors in C#. But unlike destructors in C++ which are automatically called when an object is no longer in use, C# destructors are only called by the Garbage Collector and we already know that, we can't determine when it will be called.

So having Garbage Collector in place and destructors in the classes, you can never be sure when the resources held by the objects will be released.

There might be scenarios in which we need to release certain resources held by an object (like a database connection) once the object is no longer in use. In such situations, we need to implement IDisposable interface which defines a method to release allocated resources. Basically the Dispose method of this interface is used to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method explicitly when the object is no longer needed.

So when writing code, developers have to take care to call the Dispose method when the object is no longer in use. Now, what if the developers who use our class forget to call the Dispose() method? To handle this, we can write the same code (which is there in the Dispose method to release the resources) in C# destructor too, so that at least the garbage collector will release the resources, whenever it is invoked.

Now what if the developer also calls the Dispose() method and garbage collector also gets invoked and calls the destructor on such objects. So we should ensure that if Dispose() method is called on an object then garbage collector does not invoke destructor on that object. This could be achieved by calling System.GC.SuppressFinalize() method at the end of Dispose() method. SuppressFinalize() tells the Garbage Collector not to call finalize method on that object.

To summarize both destructor and dispose method contains code to release the resources held by an object, but we cannot call destructor explicitly and it is only called by the Garbage Collector. On the other hand, Dispose method can be called explicitly when the object is no longer needed.


Viewing all articles
Browse latest Browse all 10

Trending Articles