none
objects accumulating into finalization queue RRS feed

  • Question

  • So, I have a windows forms app, linked against .Net 4.6, where the finalization queue keeps growing (hundreds of thousands of objects).

    The stack trace for the finalizer thread is:

    00000000`1aa2f808 000007fe`fd981420 ntdll!ZwWaitForMultipleObjects+0xa
    00000000`1aa2f810 00000000`778f1273 KERNELBASE!WaitForMultipleObjectsEx+0xe8
    00000000`1aa2f910 000007fe`e3ca14da KERNEL32!WaitForMultipleObjectsExImplementation+0xb3
    00000000`1aa2f9a0 000007fe`e3c35814 clr!FinalizerThread::WaitForFinalizerEvent+0xb6
    00000000`1aa2f9e0 000007fe`e3ae7ce1 clr!FinalizerThread::FinalizerThreadWorker+0x54
    00000000`1aa2fa20 000007fe`e3ae7c50 clr!ManagedThreadBase_DispatchInner+0x39
    00000000`1aa2fa60 000007fe`e3ae7b8d clr!ManagedThreadBase_DispatchMiddle+0x6c
    00000000`1aa2fb60 000007fe`e3b8a1aa clr!ManagedThreadBase_DispatchOuter+0x75
    00000000`1aa2fbf0 000007fe`e3bd3f7f clr!FinalizerThread::FinalizerThreadStart+0x126
    00000000`1aa2fc90 00000000`778e556d clr!Thread::intermediateThreadProc+0x86
    00000000`1aa2fd50 00000000`77b4385d KERNEL32!BaseThreadInitThunk+0xd
    00000000`1aa2fd80 00000000`00000000 ntdll!RtlUserThreadStart+0x1d

    To my untrained eye, this looks like the finalizer thread is not stuck executing a blocking finalizer. Instead it seems to me that it is just waiting for objects to finalize. Am I wrong?

    I've tried injecting some WaitForPendingFinalizers into the code, which makes no difference.

    Any help would be greatly appreciated. I've been googling the topic for a few hours, but all the explanations I found revolved around blocking finalizers.

    Monday, September 30, 2019 10:38 PM

Answers

  • From the callstack of the finalizer: it is waiting, so there are no finalizable objects.
    Yes, the finalizer queue is containing finalizable objects. But as long as they are kept alive they will not be finalized.
    You could use !gcroot to find what is keeping one of the objects alive.

    • Marked as answer by Luc Habert Tuesday, October 1, 2019 8:42 PM
    Tuesday, October 1, 2019 7:32 PM
  • Finalizers (C# Programming Guide)

    When a class contains a finalizer, an entry is created in the Finalize queue. 

    • Marked as answer by Luc Habert Friday, October 4, 2019 6:14 PM
    Wednesday, October 2, 2019 4:11 AM

All replies

  • Hi Luc Habert, 

    Thank you for posting here.

    According to your description, I have a question to confirm with you.

    Could you provide the related code including which line of the code throw the exception? It will help us to analyze your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 1, 2019 6:06 AM
    Moderator
  • No exception is thrown. This is a backtrace of the finalizer thread captured with windbg after pausing the exe.

    I'm providing it because the usual explanation for the symptom I get (object accumulation in finalizer queue) is that the finalizer thread is executing a finalizer that is stuck, which prevents finalizing other objects. I don't think it's the case here, seeing as it doesn't seem to be executing any finalizer (but I could be wrong, I have essentially zero knowledge of this topic).

    I can't provide the code to you, it's huge and I haven't been able to reproduce the problem on a minimal case.

    • Edited by Luc Habert Tuesday, October 1, 2019 10:35 AM
    • Marked as answer by Luc Habert Tuesday, October 1, 2019 8:42 PM
    • Unmarked as answer by Luc Habert Tuesday, October 1, 2019 8:42 PM
    Tuesday, October 1, 2019 10:32 AM
  • From the callstack of the finalizer: it is waiting, so there are no finalizable objects.
    Yes, the finalizer queue is containing finalizable objects. But as long as they are kept alive they will not be finalized.
    You could use !gcroot to find what is keeping one of the objects alive.

    • Marked as answer by Luc Habert Tuesday, October 1, 2019 8:42 PM
    Tuesday, October 1, 2019 7:32 PM
  • Thanks a lot EckiS!

    I've been able to find and eliminate the leaks thanks to gcroot.

    I had previously attempted with perfview, which would only write this

    + [finalization handles]
     + [.NET Roots]
      + ROOT

    in the refrerred from tree, without showing me the paths to the root
    reported by gcroot in windbg. Any idea why perfview failed to identify the
    leaks?

    Another question for my education: I believed that objects were inserted into the finalization queue by the GC when discovered as unreachable. I now understand that this is not the case. At what point are objects inserted into the finalization queue? At the time of their creation?

    • Edited by Luc Habert Tuesday, October 1, 2019 10:42 PM
    Tuesday, October 1, 2019 10:42 PM
  • Finalizers (C# Programming Guide)

    When a class contains a finalizer, an entry is created in the Finalize queue. 

    • Marked as answer by Luc Habert Friday, October 4, 2019 6:14 PM
    Wednesday, October 2, 2019 4:11 AM