Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How GC works with double linked list

 
Rakoczi Markus
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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?

Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic