Garbage collection of Composed Objects

前端 未结 5 2243
青春惊慌失措
青春惊慌失措 2021-01-05 11:10

I have two classes

Class A
{
    //constructor
}

Class B
{
    private A a;

    public B()
    {
        a = new A();
    }
}

Suppose I u

5条回答
  •  别那么骄傲
    2021-01-05 11:25

    The GC is smart enough to follow paths in graphs of objects in order to check if the existing references to some object are reachable. It can even detect cycles (like, in your case, if b held a reference to a and a held a reference to b.

提交回复
热议问题