This is an interview question but I'm not quite sure about the correct answer. Suppose we have some classes like:
public class A {
public Object link;
public A() {
link = null;
}
}
Then we create two instances:
A a1 = new A();
A a2 = new A();
a1.link = a2;
a2.link = a1;
Then we release the references:
a1 = null;
a2 = null;
Then the question is: as JVM would use GC mechanism. How would it handle this case? Will it instantly remove the two instances when it runs or just have the memory space signed and leave them alone? What if I have 1 million such instances that form a cycle and have no external references? Will the cleaning makes the GC thread hang?