tags:

views:

137

answers:

3

I want to know about unmanaged resources. Can anyone please give me a basic idea?

+1  A: 

The basic difference between a managed and unmanaged resource is that the garbage collector knows about all managed resources, at some point in time the GC will come along and clean up all the memory and resources associated with a managed object. The GC does not know about unmanaged resources, such as files, stream and handles, so if you do not clean them up explicitly in your code then you will end up with memory leaks and locked resources.

Stolen from here, feel free to read the entire post.

Marko
+2  A: 

Unmanaged resources are those that run outside the .NET runtime (CLR)(aka non-.NET code.) For example, a call to a DLL in the Win32 API, or a call to a .dll written in C++.

David Stratton
+4  A: 

Managed resources basically means "managed memory" that is managed by the garbage collector. When you no longer have any references to a managed object (which uses managed memory), the garbage collector will (eventually) release that memory for you.

Unmanaged resources are then everything that the garbage collector does not know about. For example:

  • Open files
  • Open network connections
  • Unmanaged memory
  • In XNA: vertex buffers, index buffers, textures, etc.

Normally you want to release those unmanaged resources before you lose all the references you have to the object managing them. You do this by calling Dispose on that object, or (in C#) using the using statement which will handle calling Dispose for you.

If you neglect to Dispose of your unmanaged resources correctly, the garbage collector will eventually handle it for you when the object containing that resource is garbage collected (this is "finalization"). But because the garbage collector doesn't know about the unmanaged resources, it can't tell how badly it needs to release them - so it's possible for your program to perform poorly or run out of resources entirely.

If you implement a class yourself that handles unmanaged resources, it is up to you to implement Dispose and Finalize correctly.

Andrew Russell
Open Database connection comes under which category?Managed / Unmanaged?
Dev
+1 Other answers miss the important point that you are calling Dispose on a *managed* object that internally handles the freeing up of the unmanaged resource it wraps (e.g. file handle, GDI+ bitmap, ...) and that if you access unmanaged resources directly (PInvoke etc.) you need to handle that.
Hightechrider
Thanks a lot everyone :)
Dev
@Dev: Unmanaged - as the GC doesn't know about it (assuming you are not using some hypothetical in-managed-memory database). But the connection object *itself* might not hold an unmanaged resource. Presumably the database connection uses an open file or network connection *somewhere* - but it is *possible* that another object (other than the connection object) is handling that unmanaged resource (perhaps your database library caches connections). Check the documentation and see where it asks you to call `Dispose` or use `using`.
Andrew Russell