Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Managed, Unmanaged, GC, Idisposable != Scary

0.00/5 (No votes)
9 Sep 2016 1  
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

  1. 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.
  2. Unmanaged Resources are those that are not under direct control of runtime, e.g. Database Connections, File handles, COM objects, etc.
  3. 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.
  4. Garbage Collector: Someone who is responsible for freeing memory which is no longer required.
  5. 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 !!)
  6. Question comes then, who'll take care of Unmanaged Resources??
  7. Here come our 2 heros (Finalizer and IDisposable)
  8. 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).
  9. 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.
  10. Since both IDisposable and Finalizer serve the same purpose of cleaning unmanaged resources, better we keep core cleaning logic in a single place.

    <img src="1063774/Capture.PNG" style="width: 570px; height: 163px; vertical-align: middle;" />

  11. 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.

    <img height="437" src="1063774/s.PNG" width="351" />

  12. 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?
  13. 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.
  14. 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 !!!
  15. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here