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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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:
The difference is likely to be irrelevant most of the time, but it's good to know :)
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.
Check this out: How does Java Garbage Collector Handle Self References.
You may want to check
java.lang.ref.WeakReference