开发者

C# garbage collector cross reference

开发者 https://www.devze.com 2022-12-29 13:13 出处:网络
Will garbage collector free resources for cross referenced object/class, which is no longer referenced from main program.

Will garbage collector free resources for cross referenced object/class, which is no longer referenced from main program. For example -

class class1
{
    class2 m_RefClass2;
}
cla开发者_StackOverflowss class2
{
    class1 m_RefClass1;
}

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


Yes. The .NET garbage collector is not simply counting references (in which case such an arrangement would cause both classes to keep each other alive). Any object that is not "rooted" (meaning that there is no reference path to the object from a GC root object) is eligible for collection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号