最终确定可达表

发布于 2024-07-27 05:13:37 字数 240 浏览 2 评论 0原文

如果我在类 Foo 中实现析构函数,则 Foo 的实例将在终结队列上被密切跟踪。 当 Foo 的实例被垃圾收集时,我知道 CLR 会看到终结队列中的条目,并通过将对象移出堆并移入终结可达表来对该对象进行特殊处理。 然后...该垃圾收集周期没有发生任何其他事情?

Finalize() 会在下一个垃圾回收周期中始终被调用吗?

为什么在将对象复制到易碎表后不立即调用 Finalize? (这似乎是额外不必要的复杂性)

If I implement a destructor in a class, Foo, instances of Foo are tracked closely on the finalization queue. When an instance of Foo is garbage collected, I understand that the CLR sees the entry in the finalization queue and gives that object special treatment by moving the object off the heap and into the finalization reachable table. Then... nothing else happens for that garbage collection cycle?

Will finalize() always be called during the next garbage collection cycle?

Why isn't finalize called immediately after copying my object to the freachable table? (this seems like extra unnecessary complexity)

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

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

发布评论

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

评论(1

他是夢罘是命 2024-08-03 05:13:37

终结器队列的作用是简化事情; 如果没有它,事情会更加复杂。 当 GC 运行时,不得执行任何托管代码 - 否则,如果用户代码在中间运行,则 GC 所做的所有分析都可能无效。

因此,当 GC 运行时,终结必须延迟,而不是立即执行。 在单独的线程中运行它可以最大限度地减少虚拟机需要独占访问所有线程的时间,并增加并发活动的可能性。

The finalizer queue is there to simplify things; it would be more complex without it. When the GC runs, no managed code must be executed - else all analysis that the GC had made might be void if user code runs in the middle.

So when the GC runs, finalization must be deferred, instead of getting executed right away. Running it in a separate thread minimizes the time that the VM requires exclusive access to all threads, and increases the potential for concurrent activities.

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