本文介绍了垃圾收集行为与孤立的循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果堆中有两个对象相互引用,但它们没有链接到任何引用变量,那么这些对象是否有资格进行垃圾回收?

If I have two objects on the heap referring to each other but they are not linking to any reference variable then are those objects eligible for garbage collection?

推荐答案

是的,他们是。基本上,GC从已知根(静态变量,alll线程中所有堆栈帧的局部变量)走到找到不能被垃圾收集的对象。如果没有办法从根目录获取对象,那么它有资格收藏。

Yes, they are. Basically the GC walks from "known roots" (static variables, local variables from all stack frames in alll threads) to find objects which can't be garbage collected. If there's no way of getting to an object from a root, it's eligible for collection.

编辑:汤姆指出了这一点,我认为这值得解答:
$ b

Tom pointed this out, which I thought was worth lifting into the answer itself:



The difference is likely to be irrelevant most of the time, but it's good to know :)

这篇关于垃圾收集行为与孤立的循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 02:30