What is meant by "managed" vs "unmanaged" resources in .NET?

C#.NetUnmanagedManaged

C# Problem Overview


What is meant by the terms managed resource and unmanaged resource in .NET? How do they come into the picture?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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: > > 1. Allows you to get rid of any resources you grabbed directly from > the operating system behind .NET's back (unmanaged resources). > > 2. 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.

Solution 4 - C#

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

Solution 5 - C#

Managed resources are resources which can be freed up by the garbage collector and unmanaged resources cannot be freed up by the garbage collector for this purpose destructor is required.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRed SwanView Question on Stackoverflow
Solution 1 - C#RobView Answer on Stackoverflow
Solution 2 - C#OdedView Answer on Stackoverflow
Solution 3 - C#DavidRRView Answer on Stackoverflow
Solution 4 - C#Sachin ShanbhagView Answer on Stackoverflow
Solution 5 - C#anilView Answer on Stackoverflow