How to list all object in GC finalization list?

天涯浪子 提交于 2020-01-03 17:26:12

问题


I have crash in my program, it is a visualizer for VS, so, it is very hard to debug it, i have tried to make dump and use WinDbg to study it, but it unsuccessful.

So, now i try to put my hands on that list programmatically, but i don't know how. Thanks.


回答1:


I do not think that there is a way to get at the finalization queue via .NET's managed Framework Class Library (FCL). I suspect that if you want to do this programmatically instead of debugging with WinDbg, you (just like WinDbg and similar tools) will need to use the CLR's unmanaged debugging & profiling APIs towards that end.

Take a look at the ICORDebugGCReferenceEnum COM interface. You can retrieve on object of that type via ICorDebugProcess5::EnumerateGCReferences:

"Provides an enumerator for objects that will be garbage-collected."

"The COR_GC_REFERENCE objects in the collection populated by [the ICorDebugGCReferenceEnum::Next method] represent three kinds of objects:

  • Objects from all managed stacks. This includes live references in managed code as well as objects created by the common language runtime.

  • Objects from the handle table. This includes strong references (HNDTYPE_STRONG and HNDTYPE_REFCOUNT) and static variables in a module.

  • Objects from the finalizer queue. The finalizer queue roots objects until the finalizer has run."

(Hyperlinks and emphasis added by me.)

Each object returned by the enumerator has a field type. You might want to filter for objects where that field matches the value CorGCReferenceType.CorReferenceFinalizer.




回答2:


If you want to see if an object is in the finalization queue or the f-reachable queue, when you fire off WinDBG, first locate your object, using dumpheap -stat or any other command. After you find that objects address, you can use the !FinalizeQueue which will output how many objects are finalizable in each generation, and how many objects are ready for finalization. The former is the finalization queue, the latter is the f-reachable queue.

For example:

0:003> !FinalizeQueue

SyncBlocks to be cleaned up: 0 MTA Interfaces to be released: 0 STA Interfaces to be released:0

generation 0 has 370 finalizable objects
(0000000000d29030->0000000000d29bc0)

generation 1 has 4 finalizable objects
(0000000000d29010->0000000000d29030)

generation 2 has 8 finalizable objects
(0000000000d28fd0->0000000000d29010)

Ready for finalization 571 objects
(0000000000d29bc0->0000000000d2ad98)

Now, you can see where your objects address space is in.

A great tutorial is available here



来源:https://stackoverflow.com/questions/30346990/how-to-list-all-object-in-gc-finalization-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!