.net – the difference between managed and native resources when disposing? (.NET)

idisposablenativenetresources

I was reading the MSDN article about how to implement IDisposable and I am uncertain about the difference between managed and native resources cited in the article.

I have a class that must dispose 2 of its fields when it is disposed. Should I treat them as Managed (dispose only when disposing = true) or Native resources?

Best Answer

To add a little to Brian's answer, and your comment/question:

The difference between a Managed/Unmanaged resource is that the Garbage Collector is aware of managed resources and isn't aware of unmanaged resources. I know that answer isn't very concrete but the difference is huge.

To help draw the line in the sand here is the short (and probably riddled with little errors) version of how GC runs and cleans up memory:

The garbage collector is aware of all managed objects but when garbage collection runs it doesn't initially know if any given object is still in use or is elegible to be released. It determines whether or not it can cleanup an object by initially marking all objects as garbage, then traversing from the application root to all referenced objects. Each object that has a relationship back to the root (a reference, either direct or indirect) gets marked as reachable and is no longer considered garbage. After the GC runs through every reachable object it cleans up the rest since they are no longer in use.

In almost all cases working with .NET framework objects you can be assured that objects are managed (.NET provides managed wrappers of nearly all unmanaged resources to ensure they are properly cleaned up); other third party components which hook into the Win32 API (or your components which do this) are the objects which may be cause for concern.

There are some .NET objects which can be considered to be somewhat unmanaged. Components of the Graphics library are one example.

Most ".NET Memory leaks" aren't really memory leaks in the true sense. Typically they occur when you think you have removed an object from use but in fact the object still has some reference to the application. A common example is adding eventhandlers (obj.SomeEvent += OnSomeEvent -or- AddHandler obj.SomeEvent, AddressOf OnSomeEvent) and not removing them.

These 'lingering references' are technically not memory leaks since your application is still technically using them; however if there are enough of them your application can suffer severe performance impacts and may show signs of resource issues (OutOfMemoryExceptions, unable to attain window handles, etc).

I'm an intermediate .NET developer and unfortunately know about these problems first-hand. I recommend playing with ANTS Profiler to help become familiar with lingering references (there is a free trial version) or if you want to get a little bit more nitty-gritty research using WinDbg and SOS.DLL to look at the managed heap. If you decide to look into the latter I recommend reading Tess Ferrandez' blog; she has a lot of great tutorials and advice on using Windbg effectively