什么是托管资源和非托管资源? [英] What are managed resources and unmanaged resources?

查看:65
本文介绍了什么是托管资源和非托管资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们什么时候应该进行内存管理,对于两种类型的资源我们该如何做?托管和非托管资源的列表是什么?我们是否真的需要担心内存泄漏?

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?

推荐答案

托管资源是指完全用.NET编写的资源.尽管通常不会发生经典的内存泄漏,但仍然可以通过不引用未使用的资源来泄漏内存(

Managed resources are those that are fully written in .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).

非托管资源通常是指那些非纯.NET(并在同一过程中)的资源-示例如下:

Unmanaged resources are those that are generally those that are not pure .NET (and in the same process) - examples are:

  • COM组件
  • 数据库连接/事务
  • 窗口句柄
  • 文件系统句柄
  • 注册表处理
  • 网络连接
  • 等...等等...

对于这些,您需要实现 正确处置模式,并在完成使用后确保正确处置.

For these, you need to implement the Dispose pattern, correctly and ensure proper disposal when you have finished using them.

我们真的需要担心内存泄漏吗?

Do we really need to worry about memory leaks or not?

是的,我们确实需要担心它们,尤其是在.NET之外的情况下.

Yes, we do need to worry about them, in particular when going outside of .NET.

这篇关于什么是托管资源和非托管资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆