Garbage collection behavior with isolated cyclic r

2020-03-17 04:18发布

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?

3条回答
▲ chillily
2楼-- · 2020-03-17 04:36

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.

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

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.

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

查看更多
爷的心禁止访问
3楼-- · 2020-03-17 04:44

Skeet's on the money, as usual. I would only add that the situation you describe is the reason that reference counting (a standard strategy with early C++ smart pointers) isn't used.

查看更多
登录 后发表回答