C # garbage collector reference

Will the garbage collector be a free resource for a cross-referenced object / class that no longer references the main program. For instance -

class class1
{
    class2 m_RefClass2;
}
class class2
{
    class1 m_RefClass1;
}

class class3
{
    public class3()
    {
        class1 obj1 = new class1();
        class2 obj2 = new class2();
        obj1.m_RefClass2 = obj2;
        obj2.m_RefClass1 = obj1;
    }
}

      

+2


a source to share


1 answer


Yes. The .NET garbage collector is about more than just reference counting (in which case, this layout will keep both classes alive). Any object that is not "root" (meaning that there is no reference path to the object from the root GC object) is eligible for collection.



+6


a source







All Articles