Managed, Unmanaged, GC, Idisposable != Scary
If (you know the basics) { things are not scary }
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
andIDisposable
) - If your class wraps any unmanaged resource, then you should have a
Finalizer
and implementIDisposable
.
IDisposable
interface has a methodDispose()
, used to clean unmanaged resources EXPLICITLY. Be very clear onEXPLICIT
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 viaIDisposable
.- Since both
IDisposable
andFinalizer
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 fromDispose()
which has no parameter. - Now, why do we need this
bool
parameter?? Before answering this, better we see it with a more interesting signatureDispose(bool explicitlyDisposing)
. Pay attention to theparam
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 byFinalizer
. - What is the conclusion then?? Simple
Dispose
's call to cleaning should pass throughDispose(true)
andFinalizer
's call to cleaning code should pass throughDispose(false)
. All done !!! - Wait... wait. Last point since in case of
IDisposable
you are callingDispose(explicitlyDisposing=true)
, which means you are not forgetting the cleaning, which again means better you stop GC to make a call toFinalizer
(which is basically a safegaurd If you forget to do cleaning explicitly viaIDisposable
). That's why it makes sense to putGC.SuppressFinalize(this)
afterDispose(true); . With that, you ensure that, System is not doing double work to clean the same thing.