如果我在堆上有两个对象相互引用但它们没有链接到任何引用变量那么那些对象是否有资格进行垃圾回收?
对,他们是.基本上,GC从“已知根”(静态变量,来自alll线程中所有堆栈帧的局部变量)走向查找无法进行垃圾回收的对象.如果无法从根目录获取对象,则可以进行收集.
编辑:汤姆指出了这一点,我认为值得提升到答案本身:
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.
差异可能在大多数时候都无关紧要,但知道:)这很好:)
翻译自:https://stackoverflow.com/questions/427363/garbage-collection-behavior-with-isolated-cyclic-references