This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
my question is about GC.
Suppose I have an acyclic double linked list like: Start<->A<->B<->C<-> ... <->NULL. If I set Start->Null, will be the rest of the list eligible for garbage collection or not?
What happens if I have a circular linked list? Should I set each object next and prev to null to be eligible for garbage collection?
As long as there are no external links to the nodes, then just setting the start to null should be okay. It may take more than one pass of the GC to collect all nodes, or it may collect them all in the same pass, depending on the implementation. But as long as there is no reachable reference to the Object, that Object is eligible for collection, regardless of the length of the 'unreachable' reference chain there might be.
And to be "reachable" doesn't just mean that there exists some reference somewhere that points to it. It has to be reachable from code that's on the stack. Java's GC doesn't use reference counting, so being an "island" of A <--> B both pointing to each other does not prevent A and B from being GCed.