Will an element be garbage collected if there is a reference to its field?

后端 未结 4 1449
粉色の甜心
粉色の甜心 2021-01-17 12:31

I.e., in

class A {
    public String s;
}

and

A a1 = new A();
a1.s = \"bla\";
A a2 = new A();
a2.s = a1.s;
a1 = null;
         


        
4条回答
  •  死守一世寂寞
    2021-01-17 12:58

    Since A has only a reference to s, a2.s pointing to a1.s would not affect a1's garbage collection.

    i.e a1 is eligible for GC, but the object referred to by a2.s (or a1.s) will not be eligible for GC.

提交回复
热议问题