Finalize和Dispose(垃圾收集)有什么区别? [英] What is the difference between Finalize and Dispose (Garbage collection) ?

查看:124
本文介绍了Finalize和Dispose(垃圾收集)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何实时使用.

How can we use in real time.

Can any one help me on this.

推荐答案

好文章分享了何时以及如何在.NET中使用finalize和dispose
.
请参阅: http://www.devx.com/dotnet/Article/33167 [
Nice article shares when and how to use finalize and dispose in .NET

Refer: http://www.devx.com/dotnet/Article/33167[^]


也请参阅我对以下内容的评论解决方案1和2.您还需要了解如何将System.IDisposable.Disposedusing语句一起使用(不要与using子句混淆):

http://msdn.microsoft.com/en-us/library/yh598w02%28v = vs.80%29.aspx [ ^ ].

有关System.IDisposable.Disposed在资源回收之外的某些用途,请参阅我的文章:
Please also see my comments to Solutions 1 and 2. You also need to understand how System.IDisposable.Disposed is used with the using statement (don''t mix up with using clause):

http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx[^].

For some use of System.IDisposable.Disposed beyond reclaiming of resources, please see my article: Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

—SA


Finalize()由Garbage Collector隐式调用,以释放非托管资源.在不再有对该对象的有效引用之后,垃圾收集器会在某个时候调用此方法.有一些资源,例如Windows句柄,数据库连接,垃圾收集器无法收集这些资源.因此,程序员需要调用IDisposable接口的Dispose()方法.
程序员调用IDisposable接口的Dispose(),以在不再使用资源时显式释放资源.即使对该对象的其他引用仍然存在,也可以调用Dispose().

有关示例的更多详细信息,请参见下面的链接:
http: //www.dotnetobject.com/expage.php?exurl=http://msdn.microsoft.com/en-us/library/b1yfkh5e%28VS.80%29.aspx [
Finalize () is called by Garbage Collector implicitly to free unmanaged resources. The garbage collector calls this method at some point after there are no longer valid references to the object. There are some resources like windows handles, database connections which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface.
Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.

For more detail with example, refer below link:
http://www.dotnetobject.com/expage.php?exurl=http://msdn.microsoft.com/en-us/library/b1yfkh5e%28VS.80%29.aspx[^]


这篇关于Finalize和Dispose(垃圾收集)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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