Introduction
I was always confused about GC, Managed, Unmanaged and the implementation of BIG Machine "IDisposable Pattern" and its internal Nut & Bolts "GC, Disposing, Disposed, Dispose(), Dispose(true), Managed, Unmanaged".
It's been long since I wanted to write about this, But before that, I myself wanted to explore what exactly those keywords meant. And now I am writing these 15 very core points so that others, having the same nightmare, could very well dispose their nightmare and get a good sleep.
Core Points
- Managed Resources are those that are pure .NET code and managed by the runtime and are under its direct control. e.g.
int
, double
, struct
, class
. - Unmanaged Resources are those that are not under direct control of runtime, e.g. Database Connections, File handles, COM objects, etc.
- While programming in the managed environment, you allocate memory on the managed heap using the new operator (
var obj = new ClassName()
), and when your work is done, then the garbage collector (GC) reclaims that memory. - Garbage Collector: Someone who is responsible for freeing memory which is no longer required.
- GC only and only know about Managed Resources. At some undeterministic point in time, the GC will come along and clean up all the memory and resources associated with a managed object (only managed object !!)
- Question comes then, who'll take care of Unmanaged Resources??
- Here come our 2 heros (
Finalizer
and IDisposable
) - If your class wraps any unmanaged resource, then you should have a
Finalizer
and implement IDisposable
.
IDisposable
interface has a method Dispose()
, used to clean unmanaged resources EXPLICITLY. Be very clear on EXPLICIT
keyword (as YOU explicitly call this method for cleaning). Finalizer
is also used to clean all unmanaged resources, BUT NOT EXPLICITLY. So you write the code of cleaning but you do not explicitly call this method. It will be called by GC. Finalizer is basically a safegaurd if you forget to do cleaning explicitly via IDisposable
. - Since both
IDisposable
and Finalizer
serve the same purpose of cleaning unmanaged resources, better we keep core cleaning logic in a single place.
- Above point 10 of keeping the logic at a single place gives birth to a new method
Dispose(bool disposing)
. See it is different from Dispose()
which has no parameter.
- Now, why do we need this
bool
parameter?? Before answering this, better we see it with a more interesting signature Dispose(bool explicitlyDisposing)
. Pay attention to the param
name "explicitlyDisposing
". Does it give any guess? - If you guessed right, then yes this variable is nothing more that just specifying whether "Unmanaged resources" are being cleaned explicitly by YOU or by GC. In more simple words, whether it's cleaned by your call to Dispose() method of
IDisposable
or by Finalizer
. - What is the conclusion then?? Simple
Dispose
's call to cleaning should pass through Dispose(true)
and Finalizer
's call to cleaning code should pass through Dispose(false)
. All done !!! - Wait... wait. Last point since in case of
IDisposable
you are calling Dispose(explicitlyDisposing=true)
, which means you are not forgetting the cleaning, which again means better you stop GC to make a call to Finalizer
(which is basically a safegaurd If you forget to do cleaning explicitly via IDisposable
). That's why it makes sense to put GC.SuppressFinalize(this)
after Dispose(true); . With that, you ensure that, System is not doing double work to clean the same thing.