java - Will these LinkedList nodes be eligible for garbage collection? -
i implementing own linkedlist
. right now, i'm on clear()
function removes of elements list.
here code far:
public class linkedlist<e> { // first element of linkedlist private node start; // last element of linkedlist private node end; // number of elements constitute linkedlist private int length; private class node { private node last; private node next; private e data; // each node has reference previous node , // next node. first element of linkedlist // have null reference previous element // , last element have null reference // next element. each node stores piece of data // in data variable. } public void clear() { start = null; end = null; length = 0; } // other methods omitted }
in clear()
function, set start
, end
references null
. unsure how affect nodes in middle of linked list (or start
, end
nodes).
if have 4 nodes (or more) in linkedlist
, middle 2 (or more) point each other. once set start
, end
null
, code doesn't have references objects anymore, have references each other.
will these middle nodes, or start , end nodes, eligible garbage collection or not, , why?
bonus question: how figure out experimentally?
seems me worrying wrong thing here. gc track roots , clear children them also. example mean linkedlist not used active threads, eligible garbage collection, if children point real data (nodes in case). use of having object not needed anymore if relates data?
linkedlist | | node1 node2
if linkedlist garbage collected, nodes, not have worry this. clear method has reset linkedlist default, empty 1 , not worry gc.
p.s. might want read how gc works, @ least understand roots.
Comments
Post a Comment