"Orphaned cycles" may not be exactly the correct term for what I'm trying to describe. Here is an example of what I'm trying to describe in code:
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?
}
}
Given a graph of references in memory containing a cycle, can the JVM garbage collect the memory references once the cycle is unreachable by code? Or is this a memory leak?