“孤立周期”可能不是我要描述的正确术语。这是我试图在代码中描述的示例:
public class Container {
private Container otherContainer;
public void setContainer(Container otherContainer) {
this.otherContainer = otherContainer;
}
}
public class Main {
public static void main(String[] args) {
doStuff();
}
public static void doStuff() {
Container c1 = new Container();
Container c2 = new Container();
c1.setContainer(c2);
c2.setContainer(c1);
//c1 and c2 now each hold a reference to each other,
//will they be garbage-collected once this method falls out of scope?
}
}
给定一个包含循环的内存引用图,一旦代码无法访问循环,JVM 是否可以垃圾收集内存引用?或者这是内存泄漏?