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

前端 未结 5 584
余生分开走
余生分开走 2020-11-28 03:29

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

相关标签:
5条回答
  • 2020-11-28 03:36

    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.

    0 讨论(0)
  • 2020-11-28 03:39

    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.

    0 讨论(0)
  • 2020-11-28 03:47

    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.

    0 讨论(0)
  • 2020-11-28 03:50

    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.

    0 讨论(0)
  • 2020-11-28 03:55

    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

    0 讨论(0)
提交回复
热议问题