Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all three garbage collection generations have a finalizer queue in .NET?

Do all three generations in the .NET GC have a finalizer queue, or does Generation 2 only have it? I am assuming that only Generation 2 has a finalizer queue and f-reachable queue associated with it.

like image 722
veena Avatar asked Oct 22 '25 05:10

veena


2 Answers

There is one queue. You can view it with SOS: http://blogs.msdn.com/b/tess/archive/2007/10/19/net-finalizer-memory-leak-debugging-with-sos-dll-in-visual-studio.aspx

like image 150
Jared Shaver Avatar answered Oct 23 '25 20:10

Jared Shaver


This is difficult to explain, the actual CLR code is not a very good match with the descriptions you'll find in the .NET literature and it is quite convoluted. There is indeed one logical structure inside the CLR with the name "finalization_queue". However, it has internal structure that organizes the finalizable objects by generation.

There's a better way to show this. If your assumption is correct, there would be no way an object can be finalized after a gen#0 collection. Try it:

using System;

class Program {
    static void Main(string[] args) {
        var obj = new Test();
        obj = null;
        Console.WriteLine("before gen#0 collection");
        GC.Collect(0);
        GC.WaitForPendingFinalizers();
        Console.WriteLine("after gen#0 collection");
        Console.ReadLine();
    }
}

class Test {
    ~Test() { Console.WriteLine("finalized"); }
}
like image 31
Hans Passant Avatar answered Oct 23 '25 20:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!