Information Processmg Letters 34 (1990) 31-35 North-Holland 22 February 1990 CYCLIC REFERENCE COUNTING WITH LOCAL MARK-SCAN Alejandro D. MARTINEZ and Rosita WACHENCHAUZER Esruela Superior Latmoamerlcana de Informcitlca, Perewa Iraola CC 3193, 1000 Buenos Aires, Argeniinu Rafael D. LINS Departamento de Informcitlca, Universidade Federal de Pernambuco, Au. Prof Luiz Freire, 50.000 Recrfe, PE, Brazil Communicated by P. Henderson Received July 1989 Keywords: Compilers, analysis of algorithms, functional programming, garbage collection Introduction The process of reclaiming unused storage space is called garbage collection. The two most simple algorithms used for garbage collection are mark- scan and reference counting. The mark-scan garbage collection algorithm works in two phases. If a machine runs out of space, the computation process is suspended and the garbage collection algorithm is called. First, the algorithm traverses all the data structures (or cells) in use putting a mark in each cell visited. Then the scan process takes place collecting all unmarked cells in a free-list. When the mark-scan process has finished, computation is resumed. The amount of time taken for garbage collection by the mark-scan algorithm is proportional to the size of the heap (the work space where cells are alloc- ated). The copying algorithm is a modified version of the mark-scan algorithm in which the heap is divided in two halves. The algorithm copies cells from one half to the other during collection. Its time complexity is proportional to the size of the graph in use. Mark-scan and copying algorithms generally traverse all the reachable data structures during garbage collection, which makes them un- suitable for real-time or large-virtual-memory ap- plications. In reference counting, each data structure or cell has an additional field which counts the num- ber of references to it, i.e., the number of pointers to it. During computation, alterations to the data structure imply changes to the connectivity of the graph and, consequently, readjustment of the value of the count of the cells involved. Reference counting has the major advantage of being per- formed in small steps interleaved with computa- tion, while other algorithms imply suspending computation for much longer. The disadvantage of the trivial algorithm for reference counting is the inability to reclaim cyclic structures. To solve this problem, a mixture of mark-scan and refer- ence counting has already been used in the past. We refer to [3] for a detailed analysis of these algorithms. More recently, several garbage collection al- gorithms have been developed for cyclic struc- tures. In [4] Friedman and Wise present an al- gorithm which can recover cyclic structures that are created in one operation, and never modified thereafter. This is the case for the cyclic represen- tation of recursive functions in Lisp and func- tional languages. Bobrow [l] gives an algorithm which can, in principle, recover all cyclic struc- tures. His method relies on explicit information provided by the programmer. Bobrow collects nodes of the graph together to form groups and associates a reference count with a group rather than an individual data structure. Hughes’ al- gorithm [5] is based on Bobrow’s. It has the major 0020-0190/90/$3.50 0 1990, Elsevier Science Publishers B.V. (North-Holland) 31