Click here to Skip to main content
15,665,942 members
Articles / Web Development / ASP.NET
Posted 12 Dec 2015


14 bookmarked

Managed, Unmanaged, GC, Idisposable != Scary

Rate me:
Please Sign up or sign in to vote.
4.62/5 (21 votes)
9 Sep 2016CPOL3 min read
If (you know the basics) { things are not scary }


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.

    Image 1

  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.

    Image 2

  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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Comments and Discussions

GeneralMy vote of 5 Pin
Anurag Gandhi12-Sep-16 19:38
professionalAnurag Gandhi12-Sep-16 19:38 
PraiseMy vote of 5 Pin
Jaytee11611-Sep-16 16:59
Jaytee11611-Sep-16 16:59 
SuggestionImportant point Pin
Nathan Minier11-Sep-16 7:13
professionalNathan Minier11-Sep-16 7:13 
One big thing that is missing is the handling of other objects with finalizers as part of the Dispose pattern. These should not be disposed if called from the finalizer (as they might already have been finalized) hence the disposing (or explicitlyDisposing) flag.

The method should look something like this:
protected virtual void Dispose(bool disposing)
      unmanagedResource.Close(); //or = null, or whatever cleanup is needed 
      _disposed = true;

There is a ton of additional information at:
Dispose Pattern[^]
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli

GeneralMy vote of 4 Pin
Imagiv11-Sep-16 6:18
Imagiv11-Sep-16 6:18 
BugIt only covers simple cases... Pin
Philippe Mori10-Sep-16 11:53
Philippe Mori10-Sep-16 11:53 
GeneralMy vote of 5 Pin
JayantaChatterjee14-Dec-15 23:14
professionalJayantaChatterjee14-Dec-15 23:14 
GeneralRe: My vote of 5 Pin
Rajita R9-Sep-16 9:08
Rajita R9-Sep-16 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.