Garbage Collection Modern programming languages provide garbage collection mechanisms for reclaiming the memory locations that are no longer used by programs One of the commonly used technique in centralised system is reference counting each object keeps a reference count when the reference to an object is copied, the reference count of the object is incremented when the reference to an object is deleted, the reference count of the object is decremented when the reference count becomes 0, the object can be garbage collected problems with reference counting when applied in a distributed system object might be garbage collected prematurely The problem is due to the messages for incrementing and decrementing count are received out of order 1 5 4 3 2 7 6 1
weighted reference counting An object has a weight assign weight to each pointer object weight = sum of the pointers’ weight when the pointer is copied, evenly distribute the weight When a pointer is deleted, the weight of the pointer is subtracted from the weight of the object An object is garbage collected when its weight becomes to 0 1. problem in an object oriented system 2. reference counting scheme 3. problems on a distributed system 4. assign weight to each pointer 5. when the pointer is copied, evnly distributed the weight 6. when the weight cannot be divided any more, create a new obj Ref counting: when a ref is deleted count is decrease when count=0 obj is garbage collect Reference can be copied: count is increased Problem is distributed 2 2 copied 3 1 delete copy Delete arrive first delete 1 Copy arrives later 1 2
2 3 5 copied 10 deleted 10 5 copied 2 3 1 copied 10 5 3 obj weight = sum of the pointer weight When pointer is copied weight is split No premature garbage collection weight only decrease, not increase Problem: when weight cannot be divided Cannot collect cyclic garbage 10 5 copied 2 3 1 copied 10 5 3
The weight in an object only decreases It won’t have the problem as the reference counting scheme What if the weight of an object cannot be split? Create an indirection object It cannot handle cyclic garbage How to form a cyclic garbage A.1 = B B.2 = C C.3 = B Free(A)
Collecting cyclic garbage This works as a complement of other garbage collection algorithms, e.g. weighted reference counting) If an object, say A, has a reference to another object, say B, then A is the predecessor of B, and B is A's successor. Let (a) O represent a set of objects, and (b) P be the set of the predecessors of the objects in O. It is easy to see that, if all the objects in O form a cyclic structure, P must be a subset of O. 5
The Algorithm A B C D E root A root object is an operating system object Let O be a set of objects which does not include any root object, and P be the set containing all the predecessors of the objects in O. If P O, then objects in O are garbage. a. garbage include the ones only referenced from a cycle A B C D E root 6
The algorithm is going to pass some probes to discover the predecessor relationship garbage include the ones only referenced from a cycle Assume token starts from B A B C D E root 7
The probes will be sent back to the initiator of the algorithm How do we know whether we have received all the probes so that we can start looking for cyclic structures? Each probe carries some weight. When a probe p is split into n probes, p1, p2, ..., pn, all the (object, predecessor) pairs in p are copied to p1, p.w = p1.w + p2.w + ... + pn.w, pi.initiator = p.initiator where 1 ≤ i ≤ n. How do we know whether we have found all the predecessors of a node? Compare the weight of the node and the sum of the weight of the references to the node (assume the weighted reference counting is used) a. receive a probe, split and send to the successor b. receive a probe with the same initiator again, send to the initiator Assume A initiates the algorithm and carries initial weight 10 A B C D E 8
review What problem does the reference counting garbage collection algorithm might have in a distributed system? Describe how the weighted reference counting garbage collection algorithm works. Why does the weighted reference counting garbage collection algorithm not have the problem encountered by the reference counting garbage collection algorithm? Understand how the algorithm for detecting cyclic garbage works. In the algorithm for collecting cyclic garbage, how do we know whether the probes have reached an object through all the predecessors of the object? [hint: the algorithm works as a complement of other garbage collection algorithms, e.g. weighted reference counting ] Use the principles of the weighted reference counting to develop an algorithm that finds a set of nodes in a wait-for graph such that the outgoing edges from the nodes in this set always end on nodes in this set. (it would be possible to detect deadlock in the OR deadlock model using this algorithm) In the algorithm for collecting cyclic garbage, how do we know whether the probes have reached an object through all the predecessors of the object? [hint: the algorithm works as a complement of other garbage collection algorithms, e.g. weighted reference counting ] To find out whether the probes reach the node through all the predecessors, we can also collect the weight the pointers (i.e. the predecessor) through which the probe arrives. Then, we can check whether the sum of the pointers through which the probes arrive is equal to the weight of the object Use the principles of the algorithm for detecting cyclic garbage to develop an algorithm that finds a set of nodes in a wait-for graph such that the outgoing edges from the nodes in this set always end on nodes in this set: The algorithm works in such a way that probes are passed along the edges in the wait-for graph. The probe is dropped if it reaches a process that is waiting for another process. If the algorithm terminates (i.e. all the probes are returned to the initiator), there is a knot.
Further reading P.Watson and I.Watson. An efficient garbage collection scheme for parallel computer architectures.PARLE'87, LNCS 259:432-443, Springer Verlag, 1987 (weighted reference counting) X.Ye , J. Keane, Collecting Cyclic Garbage in Distributed Systems, Proceedings of the 1997 International Symposium on Parallel Architectures, Algorithms and Networks