What does `garbage' mean here? It donotes a piece of memory which, allocated before and not freed by the user, can not be accessed any more since the link to it is lost. p = new Node(...); p = new Node(...); /* allocate again, the link to the old one is lost. */ p = new Node(...); q = new Node(...); p = q; /* p now link to another node, the link to the original one is lost. */ /* In Java, a string is immutable. So every assignment actually create a new /* string and the reference to it is assigned to the varible. */ s = ""; /* s links to an empty string */ while(.....) s = s + ....; /* Each time the assignment executed, s links to a new string. Old link is lost. */ Basic Assumption: All actions of allocation and deallocation, either done explicitly or implicitly, should be monitored by the management system and the system should has access to all the live links. explicitly: p = new Node(..); /* explicitly invocation of allocation */ free(p); /* explicitly invocation of deallocation */ implicitly: p = q; /* p's old link is lost. The reference count to the object orginally pointed by p should be decreased. */ live links: p = new Tree(...); /* The system should know all the variables like q = new Tree(...); /* p, q which may contain links to another objs. p.right = new Tree(...); /* The linked objects may also contains links. p.left = new Tree(...); q = p.right; How to collect it? 1) reference count Each object is with a count which is increased by one when a new link points to it and decreased by one when a link to it is lost. p = new Node(....); Node's ref count = 1 (pointed by p) q = p; Node's ref count = 2 (pointed by q) p = new Node(....); old Node's ref count = 1 q = p; old Node's ref count = 0 (ok to collect it) p = new Node(...); Node's ref count = 1 p.next = p; Node's ref count = 2 p = q; Node's ref count = 1, (garbage!) 2) mark-sweep algorithm. phase 1: mark all objects phase 2: clear the marks of objects which can be accessed through the live links phase 3: collect the objects which is still marked.