黑白析构函数和 Finalize 方法的区别

发布于 2024-08-21 16:13:37 字数 122 浏览 2 评论 0原文

我想知道这些函数的调用顺序。就像如果我们的堆已满,GC 将被调用。它将标记该对象并调用其终结操作,现在我们有清理阶段..其中对该对象的引用被删除并且对象变得不可访问.. 那么,破坏在哪里循环进行……当它被称为……那么它在做什么……

I want to know the sequence of how these function are called. Like if our heap is full, GC will be called. It will mark the object and call its finalise operation, now we have sweep stage.. in which the reference to that object is deleted and object becomes inaccessible..
So where does the destruction come in cycle...When wld it be called..What wld it be doing then....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生面具三千个 2024-08-28 16:13:37

我必须推荐一下这个的来源。 C# 编译器团队的 Eric Lippert 最近就这个主题发表了一篇文章: 之间有什么区别析构函数和终结器?

从它们公认的定义来看,它们在 C# 中实际上是倒退的。读了他的文章,我无法更好地描述它。

I have to recommend the source on this one. Eric Lippert from the C# compiler team made a recent post on this exact subject: What’s the difference between a destructor and a finalizer?

From their accepted definitions, they're actually backwards in C#. Read his post though, I can't describe it any better.

风吹短裙飘 2024-08-28 16:13:37

C#/.NET 中没有“析构函数”,尽管该术语过去似乎与“终结器”互换使用。

大多数时候,您也不应该实现终结器;您需要使用它们的主要实例是类是否保留非托管资源,在这种情况下您应该实现 IDisposable 模式(从技术上讲,您可以调用 Dispose一个“析构函数”,但它实际上释放堆上的任何内存,这是讨论内存管理时的一个重要区别)。

不要在内存管理方面对 GC 进行事后猜测。只需确保释放您使用的所有非托管资源(文件句柄等)。如果您看到有人使用术语析构函数,他们的意思可能是终结器

There is no "destructor" in C#/.NET, although the term seems to have been used interchangeably with "finalizer" in the past.

Most of the time, you shouldn't be implementing finalizers either; the main instance where you need to use those is if the class holds on to unmanaged resources, in which case you should be implementing the IDisposable pattern (technically, you could call Dispose a "destructor", but it does not actually free any memory on the heap, which is an important distinction when discussing memory management).

Don't second-guess the GC in terms of memory management. Just make sure you release any unmanaged resources you use (file handles, etc.). And if you see someone using the term destructor, they probably meant to say finalizer.

无悔心 2024-08-28 16:13:37

A Very good video about .Net Memory management is at .Net Memory Management. It will help clear your doubts about finalizer and when it is called, how it is called, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文