0

In Java, if I have a linked list and I set its head = null, will the garbage collector be able to collect the whole list. As in this case just the head is losing reference but the nodes after head have a reference because of the previous nodes, so do they also get garbage collected?

Thanks in advance.

  • 1
    GC in java is capable of identifying and collecting so called *islands of isolation*: https://stackoverflow.com/questions/792831/island-of-isolation-of-garbage-collection – Ivan Mar 29 '18 at 18:28
  • Static objects, and any objects identified by a function parameters or local variable, are always reachable. Any non-null references stored in reachable objects identify objects which are also reachable. The behavior of a typical GC may be viewed like an automated bowling-alley pinsetter after the first ball is thrown: it picks up all reachable objects [standing pins], *blindly* sweeps up everything else, and then sets down the reachable objects. The GC need not know nor care about islands of islolation--they'll simply get swept up like everything else. – supercat Mar 29 '18 at 18:28

0 Answers0