简体   繁体   English

什么是托管资源和非托管资源?

[英]What are managed resources and unmanaged resources?

When should we do memory management and How do we do it for both the types of resources? 我们什么时候应该进行内存管理?对于两种资源我们该如何做? What is the list of managed and unmanaged resources? 托管和非托管资源的列表是什么? Do we really need to worry about memory leaks or not? 我们是否真的需要担心内存泄漏?

Managed resources are those that are fully written in .NET. 托管资源是完全用.NET编写的资源。 Though not normally subject to classic memory leaks, one can still leak memory by not dereferencing unused resources ( the most common reason is to not un-register event handlers ). 尽管通常不会发生经典的内存泄漏,但仍然可以通过不引用未使用的资源来泄漏内存( 最常见的原因是不注销事件处理程序 )。

Unmanaged resources are those that are generally those that are not pure .NET (and in the same process) - examples are: 非托管资源通常是那些不是纯.NET的资源(在同一过程中)-示例如下:

  • COM components COM组件
  • Database connections/transactions 数据库连接/事务
  • Window handles 窗把手
  • Filesystem handles 文件系统句柄
  • Registry handles 注册表句柄
  • Network connections 网络连接
  • etc... etc... 等...等...

For these, you need to implement the Dispose pattern, correctly and ensure proper disposal when you have finished using them. 对于这些,您需要正确实现Dispose模式,并在完成使用后确保正确处理。

Do we really need to worry about memory leaks or not? 我们是否真的需要担心内存泄漏?

Yes, we do need to worry about them, in particular when going outside of .NET. 是的,我们确实需要担心它们,尤其是在.NET之外的情况下。

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

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