如果我在堆上有两个相互引用的对象,但它们没有链接到任何引用变量,那么这些对象是否符合垃圾回收条件?
最佳答案
是的,他们是。基本上,GC 从“已知根”(静态变量,所有线程中所有堆栈帧中的局部变量)开始寻找不能被垃圾收集的对象。如果无法从根获取对象,则它符合收集条件。
编辑:汤姆指出了这一点,我认为值得将其纳入答案本身:
Technically, static variables are not roots - they are referenced by classes which are referenced by class loaders which are referenced by classes which are referenced by object which are referenced by root references.
在大多数时间里,差异可能是无关紧要的,但知道这一点是件好事:)
关于java - 具有独立循环引用的垃圾收集行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/427363/