What is meant by the terms managed resource and unmanaged resource in .NET? How do they come into the picture?
5 Answers
The term "unmanaged resource" is usually used to describe something not directly under the control of the garbage collector. For example, if you open a connection to a database server this will use resources on the server (for maintaining the connection) and possibly other non-.net resources on the client machine, if the provider isn't written entirely in managed code.
This is why, for something like a database connection, it's recommended you write your code thusly:
using (var connection = new SqlConnection("connection_string_here"))
{
// Code to use connection here
}
As this ensures that .Dispose()
is called on the connection object, ensuring that any unmanaged resources are cleaned up.

- 45,296
- 24
- 122
- 150
-
27I'd clarify it slightly: an "unmanaged resource" is something the garbage-collector won't know how to clean up after if it's abandoned. A short-lived object's subscription to an event from a long-lived object, for example, would be an unmanaged resource even though both objects are under the control of the garbage-collector, since the GC will have no way of knowing that the subscription should be scrapped if the subscriber is abandoned but the publisher is not. If an unbounded number of subscribers could be created and abandoned during the life of the publisher, that would cause a memory leak. – supercat Aug 12 '11 at 19:47
-
14Adding a bit more clarification: SqlConnection (or FileStream, etc) are managed resources which internally use unmanaged resources which GC is unaware of. – jimvfr Jan 17 '13 at 02:21
-
3jimvfr is right, SqlConnection is a example of managed resources. A example of unmanaged resources is when we need to allocate memory from the unmanaged memory using the method Marshal.AllocHGlobal() it's an unmanaged resource in this case the best practice is uses a destructor (~ ctor) and call the Marshal.FreeHGlobal() to release this memory. – Ygor Thomaz Jul 30 '14 at 17:13
-
can you please give an example for both managed and unmanaged resources. – Radha Manohar Jan 10 '20 at 08:39
Managed resources are those that are pure .NET code and managed by the runtime and are under its direct control.
Unmanaged resources are those that are not. File handles, pinned memory, COM objects, database connections etc.

- 489,969
- 99
- 883
- 1,009
In the Q&A What are unmanaged resources?1, Bruce Wood posted the following:
I think of the terms "managed" and "unmanaged" this way:
"Managed" refers to anything within the .NET sandbox. This includes all .NET Framework classes.
"Unmanaged" refers to the wilderness outside the .NET sandbox. This includes anything that is returned to you through calls to Win32 API functions.
If you never call a Win32 API function and never get back any Win32 "handle" objects, then you are not holding any unmanaged resources. Files and streams that you open via .NET Framework class methods are all managed wrappers.
Comment: You may not be holding an unmanaged resource directly. However, you may be holding an unmanaged resource indirectly via a managed "wrapper class" such as System.IO.FileStream. Such a wrapper class commonly implements IDisposable (either directly or via inheritance).
...many managed (.NET Framework) objects are holding unmanaged resources inside them, and you probably want to Dispose() of them as soon as you can, or at least offer your callers the opportunity to do so. That's where writing your own Dispose() method comes in. Essentially, implementing IDisposable() does two things for you:
Allows you to get rid of any resources you grabbed directly from the operating system behind .NET's back (unmanaged resources).
Allows you and your callers to release hefty .NET objects / .NET objects that are holding precious resources in their grubby little hands that you / your callers want released now.
Comment: By implementing IDisposable
and thereby providing a Dispose()
method, you are enabling a user of your class to release in a deterministic fashion any unmanaged resources that are held by an instance your class.
1 Link originally shared in Sachin Shanbhag's answer. Quoted material dated 2005-11-17. Note that I have lightly copy-edited the quoted content.
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.
For more details - http://bytes.com/topic/c-sharp/answers/276059-what-unmanaged-resources

- 54,530
- 11
- 89
- 103
-
2"The idea behind the IDisposable interface is to let you clean up resources in a deterministic fashion and clean up unmanaged resources."awesome! – zionpi Nov 14 '13 at 06:53