.net - 易碎队列和完成队列

标签 .net memory-management memory-leaks garbage-collection garbage

易碎队列和终结队列之间有什么区别?
一种解决方案:Transition from Finalization Queue to FReachable Queue .net Garbage Collection

最佳答案

这两个队列都是为了管理可终结对象。

引用:What do you know about Freachable queue?

Freachable what? You might ask. Freachable (pronounced F-reachable) is one of CLR Garbage Collector internal structures that is used in a finalization part of garbage collection. You might have heard about the Finalization queue where every object that needs finalization lands initially. This is determined based on whether he has a Finalize method, or it’s object type contains a Finalize method definition to speak more precisely. This seems like a good idea, GC wants to keep track of all objects that he needs to call Finalize on, so that when he collects he can find them easily. Why would he need another collection then?

Well apparently what GC does when he finds a garbage object that is on Finalizable queue, is a bit more complicated than you might expect. GC doesn’t call the Finalize method directly, instead removes object reference from Finalizable queue and puts it on a (wait for it.. ) Freachable queue. Weird, huh? Well it turns out there is a specialized CLR thread that is only responsible for monitoring the Freachable queue and when GC adds new items there, he kicks in, takes objects one by one and calls it’s Finalize method. One important point about it is that you shouldn’t rely on Finalize method being called by the same thread as rest of you app, don’t count on Thread Local Storage etc.

But what interest me more is why? Well the article doesn’t give an answer to that, but there are two things that come to my mind. First is performance, you obviously want the garbage collection to be as fast as possible and a great deal of work was put into making it so. It seems only natural to keep side tasks like finalization handled by a background thread, so that main one can be as fast a possible. Second, but not less important is that Finalize is after all a client code from the GC perspective, CLR can’t really trust your dear reader implementation. Maybe your Finalize will throw exception or will go into infinite loop? It’s not something you want to be a part of GC process, it’s much less dangerous if it can only affect a background thread.

关于.net - 易碎队列和完成队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32206532/

相关文章:

c# - 条件编译题

c++ - 套接字内存泄漏

c++ - 有unique_from_this()吗?或者如何从继承自 enable_shared_from_this 的类返回 unique_ptr

memory-leaks - 当循环引用的访问路径被破坏时,Java 垃圾收集器如何处理循环引用?

c# - 我们如何将一个 DataTable 的列数据复制到另一个 DataTable,即使 DataTable 之间的列名不同?

.net - 如何强制我的 C# 应用程序立即确认 TCP 数据报?

objective-c - 无法弄清楚内存泄漏从何而来

java - 如何检查 ConcurrentLinkedQueue 是否为 GC 留下垃圾(取消引用的实例)?

.net - 在 Windows 服务的上下文中删除和卸载有什么区别?

c - 嵌入式设备的内存分配/释放