.net 中的 finalize 和 dispose 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3645521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:42:13  来源:igfitidea点击:

What is the difference between finalize and dispose in .net?

.netgarbage-collectiondisposefinalize

提问by HotTester

Possible Duplicate:
Finalize vs Dispose

可能的重复:
Finalize vs Dispose

Hi,

你好,

Recently I was quizzed in an interview about finalize and dispose. When is each one of them used and how is Garbage Collector related to them. Kindly share links to enlighten more on the topic.

最近我在一个关于 finalize 和 dispose 的采访中被问到了。它们中的每一个何时使用以及垃圾收集器与它们有何关联。请分享链接以启发有关该主题的更多信息。

Kindly share ...

请分享...

Thanks in advance.

提前致谢。

回答by Joey

Finalizers are run by the Garbage Collection before an object that is eligible for collectionis reclaimed. Dispose()is meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, &c. It works best in conjunction with the usingblock where the compiler makes sure that Dispose()will be called immediately once you are done with an object – and also ensures that you cannot work with the object anymore once it's disposed.

在回收符合收集条件的对象之前,垃圾收集器会运行终结器。Dispose()用于清理非托管资源,如网络连接、文件、操作系统的句柄等。它与using编译器确保在Dispose()您完成对象后立即调用的块结合使用效果最佳- 并确保一旦对象被释放就无法再使用该对象。

Note that finalizers don't have torun, so relying on that can be dangerous:

需要注意的是终结不具有运行,因此依靠这可能是危险的:

What this means for you:Your programs cannot rely on finalizers keeping things tidy. Finalizers are a safety net, not a primary means for resource reclamation. When you are finished with a resource, you need to release it by calling Closeor Disconnector whatever cleanup method is available on the object. (The IDisposableinterface codifies this convention.)

这对您意味着什么:您的程序不能依赖终结器来保持整洁。Finalizers 是一个安全网,而不是资源回收的主要手段。当您使用完资源后,您需要通过调用CloseDisconnect对象上可用的任何清理方法来释放它。(IDisposable界面编纂了这个约定。)

Careful also with the precise time when an object becomes eligible for collection. Read the article linked above – it's neither scope (a weird word which has noting to do with the lifetime of an object – it's “the region of program text in which it is legal to refer to [a named entity] by its unqualified name.”) nor is it strictly reference counting as an object can become eligible for collection even before the last reference to it goes away.

还要注意对象符合收集条件的准确时间。阅读上面链接的文章——它既不是范围(一个与对象的生命周期无关的奇怪词——它是“程序文本的区域,在该区域中,通过非限定名称引用[命名实体]是合法的。 ”) 也不是严格的引用计数,因为即使在对它的最后一个引用消失之前,对象也有资格被收集。

回答by Darin Dimitrov

  1. Finalize: undeterministicnondeterministic destructor/finalizercalled automatically by the Garbage Collector when there are no more references to this instance.
  2. Dispose: deterministically called by the developer on an object implementing IDisposableto free resources.
  1. 最终确定:undeterministic不确定性的析构函数/终结被垃圾收集器自动调用的时候有这个实例中没有更多的参考。
  2. Dispose:由开发人员在实现IDisposable的对象上确定性地调用以释放资源。