简体   繁体   English

.NET中“托管”与“非托管”资源的含义是什么?

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

What is meant by the terms managed resource and unmanaged resource in .NET? .NET中托管资源和非托管资源的含义是什么意思? How do they come into the picture? 他们是如何进入画面的?

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. 例如,如果打开与数据库服务器的连接,则将使用服务器上的资源(用于维护连接)以及客户端计算机上的其他非.NET资源(如果提供程序不是完全使用托管代码编写的话)。

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. 这样可以确保在连接对象上调用.Dispose() ,确保清除所有非托管资源。

Managed resources are those that are pure .NET code and managed by the runtime and are under its direct control. 托管资源是那些纯.NET代码并由运行时管理并受其直接控制的资源。

Unmanaged resources are those that are not. 非托管资源是那些不受管理的资源。 File handles, pinned memory, COM objects, database connections etc. 文件句柄,固定内存,COM对象,数据库连接等。

In the Q&A What are unmanaged resources? 在问答中什么是非托管资源? 1 , Bruce Wood posted the following: 1Bruce Wood发布了以下内容:

I think of the terms "managed" and "unmanaged" this way: 我这样想到“托管”和“非托管”这两个术语:

"Managed" refers to anything within the .NET sandbox. “托管”是指.NET沙箱中的任何内容。 This includes all .NET Framework classes. 这包括所有.NET Framework类。

"Unmanaged" refers to the wilderness outside the .NET sandbox. “非托管”是指.NET沙箱之外的荒野。 This includes anything that is returned to you through calls to Win32 API functions. 这包括通过调用Win32 API函数返回给您的任何内容。

If you never call a Win32 API function and never get back any Win32 "handle" objects, then you are not holding any unmanaged resources. 如果您从未调用Win32 API函数并且永远不会返回任何Win32“句柄”对象,那么您不会持有任何非托管资源。 Files and streams that you open via .NET Framework class methods are all managed wrappers. 通过.NET Framework类方法打开的文件和流都是托管包装器。

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 . 但是,您可能通过托管的“包装类”(如System.IO.FileStream) 间接持有非托管资源。 Such a wrapper class commonly implements IDisposable (either directly or via inheritance). 这样的包装类通常实现IDisposable (直接或通过继承)。

...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. ...许多托管(.NET Framework)对象都在其中包含非托管资源,您可能希望尽快Dispose(),或者至少为您的调用者提供这样做的机会。 That's where writing your own Dispose() method comes in. Essentially, implementing IDisposable() does two things for you: 这就是编写自己的Dispose()方法的地方。实际上,实现IDisposable()会为你做两件事:

  1. Allows you to get rid of any resources you grabbed directly from the operating system behind .NET's back (unmanaged resources). 允许您从.NET背面的操作系统(非托管资源)中删除您直接获取的任何资源。

  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 . 允许您和您的呼叫者释放大量的.NET对象/ .NET对象,这些对象在您/您的呼叫者现在要释放的肮脏的小手中拥有宝贵的资源。

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. 注释:通过实现IDisposable并从而提供Dispose()方法,您可以使您的类的用户以确定的方式释放由您的类实例持有的任何非托管资源。


1 Link originally shared in Sachin Shanbhag's answer . 1链接最初在Sachin Shanbhag的回答中分享。 Quoted material dated 2005-11-17. 引用的材料日期为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. 托管资源和非托管资源之间的基本区别在于垃圾收集器知道所有托管资源,在某个时间点GC将出现并清理与托管对象关联的所有内存和资源。 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. GC不知道非托管资源,例如文件,流和句柄,因此如果您不在代码中明确清除它们,那么最终会导致内存泄漏和锁定资源。

For more details - http://bytes.com/topic/c-sharp/answers/276059-what-unmanaged-resources 有关详细信息,请访问: http://bytes.com/topic/c-sharp/answers/276059-what-unmanaged-resources

托管资源是可以由垃圾收集器释放的资源,并且垃圾收集器不能释放非托管资源,因此需要析构函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM