Suppose I have a circular reference like so:
class A {
B b = new B(this);
}
class B {
A a;
B(A a) {
this.a = a;
}
}
When an instance of A
and the instance of B
it references become unreachable, I understand that they will eventually become garbage collected. However, would they be garbage collected sooner if B
didn't reference A
back?
My concern is based on this note in the Python docs:
cycles are automatically reclaimed when garbage collection is enabled and they become unreachable, but it remains more efficient to avoid creating cycles.
My limited understanding is that this is because CPython also uses reference counting which can reclaim objects sooner when there are no cycles, but I'm not sure. I see that at least some implementations of Java don't use reference counting so this wouldn't apply. But really I don't know what I'm talking about and would like some confirmation.