析构函数和终结器的区别? [英] The difference between a destructor and a finalizer?

查看:1042
本文介绍了析构函数和终结器的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这个问题是关于中的话析构函数和终结和他们正确使用的术语之间的区别。我也只是提供了其在C#和C ++ / CLI使用的例子来证明为什么我问的问题。我清楚地知道它是如何在C#和CLR的实现,但我问的是正确使用的术语。


在C#世界术语析构函数和终结似乎要使用pretty多互换,我怀疑是因为C#规范用字析构函数,描述了非确定性的清理功能,而CLR的文档始终使用单词终结,所以C#的领域内,他们的意思是一样的。

In the C# world the terms "destructor" and "finalizer" seem to be used pretty much interchangeably, which I suspect is because the C# specification describes the non-deterministic cleanup functionality using the word "destructor", whereas the CLR documentation always uses the word "finalizer", so within the realms of C# they mean the same thing.

然而,在C / CLI规范有两者之间作出区分。它同时允许确定性和非确定性的清理,并使用该确定的功能的非确定性的功能的术语析构和终结:

However, in the C++/CLI specification there is a distinction made between the two. It allows both deterministic and non-deterministic cleanup, and uses the term "destructor" for the deterministic functionality and "finalizer" for the non-deterministic functionality:

终结提供非确定性清理。终结是垃圾收集过程中执行的最后机会的功能,通常一个对象的析构函数没有被执行的。

The finalizer provides non-deterministic cleanup. A finalizer is a "last-chance" function that is executed during garbage collection, typically on an object whose destructor was not executed.

此外析构函数并的finalizer 表明,析构函数和终结是不同的概念,并支持C ++ / CLI规范的使用条款的有关决定:

Additionally the Wikipedia descriptions of destructor and finalizer indicate that destructors and finalizers are separate concepts, and supports the C++/CLI spec's use of the terms with regard to determinism:

与析构函数,终结不确定性。当程序明确释放的对象的析构函数运行。一个终结,相反,是在内部的垃圾回收系统释放的对象执行。

Unlike destructors, finalizers are not deterministic. A destructor is run when the program explicitly frees an object. A finalizer, by contrast, is executed when the internal garbage collection system frees the object.

该问题:


  • 有,从计算机科学的角度,一个析构函数和终结之间,或者一个明确的区别是术语的东西,只能根据上下文定义?

  • Is there, from a computer science point of view, a clearly defined difference between a "destructor" and a "finalizer", or is the terminology something that can only be defined contextually?

如果有一个明确的区别,那么为什么C#的规范使用错误的术语?

If there is a clearly defined difference, then why would the C# spec use the 'wrong' terminology?

推荐答案

1)是否有析构函数和终结之间定义良好的区别在工业或学术界使用?

1) Is there a well-defined difference between "destructor" and "finalizer" as used in industry or academia?

有一定似乎是。所不同的似乎是析构函数被调用确定性清除方法,而终结时运行垃圾收集告诉他们。

There certainly appears to be. The difference seems to be that destructors are cleanup methods that are invoked deterministically, whereas finalizers run when the garbage collector tells them to.

2)在这种情况下,C#规范得到它错了 - 终结被称为析构函数在C#。没有了C#规范的作者为何弄错了吗?

2) In that case, the C# spec gets it wrong -- finalizers are called "destructors" in C#. Why did the authors of the C# spec get it wrong?

我不知道,但我可以猜。其实,我有两个猜测。

I don't know, but I can guess. In fact, I have two guesses.

猜猜#1是,在1999年5月12日没有一个维基百科文章描述清楚这两个概念之间的细微差别。这是因为没有一个维基百科。记得回来时,没有一个维基百科?黑暗时代的人。该错误可能只是一直一个诚实的错误,认为这两个术语是相同的。

Guess #1 is that on May 12th, 1999 there was not a wikipedia article clearly describing the subtle difference between these two concepts. That's because there wasn't a wikipedia. Remember back when there wasn't a wikipedia? Dark ages, man. The error might simply have been an honest mistake, believing that the two terms were identical.

哎呀,就我所知,这两个词的的在1999年5月12日,并在定义的区别一样只是后来演变,因为很明显,有必要急于之间的歧义和懒惰的清理方法。

Heck, for all I know, the two terms were identical on May 12th, 1999, and the difference in definitions only evolved later, as it became obvious that there was a need to disambiguate between eager and lazy cleanup methods.

猜测#2是,在1999年5月12日,在语言设计委员会想离开打开了析构函数可能的东西的其他的不是一个终结执行的可能性。也就是说,在析构函数被设计为一个C#语言的概念,并不一定映射一到一个具有.NET终结的概念。当设计在相同的时间,因为它上面坐也正在设计的框架的语言,有时要隔离自己免受在子系统了最新的设计变更。

Guess #2 is that on May 12th, 1999, the language design committee wished to leave open the possibility that a "destructor" could be implemented as something other than a finalizer. That is, the "destructor" was designed to be a C# language concept that did not necessarily map one-to-one with the .NET "finalizer" concept. When designing a language at the same time as the framework it sits atop is also being designed, sometimes you want to insulate yourself against late-breaking design changes in your subsystems.

语言委员会的1999年5月12日的笔记中部分内容如下:

The language committee's notes for May 12th 1999 read in part:

我们打算使用的术语
  析构函数为其执行会员
  当一个实例被回收。类
  可以有析构函数;结构不能。
  不像在C ++中,析构函数不能
  明确要求。破坏
  非确定性 - 你不能可靠地
  知道什么时候该析构函数将执行,
  只是说,它执行一些
  对所有参考点后
  对象已被释放。该
  继承链中的析构函数
  被称为在顺序,从最
  传人至少后裔。那里
  没有必要(且没有办法)为
  派生类中显式调用
  基地析构函数。 C#编译器
  析构函数编译到
  适当的CLR重新presentation。对于
  这个版本这可能意味着一个
  例如终结是
  区分元数据。 CLR可能
  提供在静态终结
  未来;我们没有看到任何障碍
  使用静态终结C#。

We're going to use the term "destructor" for the member which executes when an instance is reclaimed. Classes can have destructors; structs can't. Unlike in C++, a destructor cannot be called explicitly. Destruction is non-deterministic – you can't reliably know when the destructor will execute, except to say that it executes at some point after all references to the object have been released. The destructors in an inheritance chain are called in order, from most descendant to least descendant. There is no need (and no way) for the derived class to explicitly call the base destructor. The C# compiler compiles destructors to the appropriate CLR representation. For this version that probably means an instance finalizer that is distinguished in metadata. CLR may provide static finalizers in the future; we do not see any barrier to C# using static finalizers.

所以,在那里,你现在知道我知道关于这个问题的一切。如果你想了解更多,请安德斯下次你看见他的时间。

So, there, you now know everything I know on the subject. If you want to know more, ask Anders next time you see him.

这篇关于析构函数和终结器的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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