11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

424 Chap. 12 Lists <strong>and</strong> Arrays RevisitedghFigure 12.17 Garbage cycle example. All memory elements in the cycle havenon-zero reference counts because each element has one pointer to it, even thoughthe entire cycle is garbage (i.e., no st<strong>at</strong>ic variable in the program points to it).Reference counts have several major disadvantages. First, a reference countmust be maintained for each memory object. This works well when the objects arelarge, such as a file. However, it will not work well in a system such as LISP wherethe memory objects typically consist of two pointers or a value (an <strong>at</strong>om). Anothermajor problem occurs when garbage contains cycles. Consider Figure 12.17. Hereeach memory object is pointed to once, but the collection of objects is still garbagebecause no pointer points to the collection. Thus, reference counts only work whenthe memory objects are linked together without cycles, such as the UNIX file systemwhere files can only be organized as a DAG.Another approach to garbage collection is the mark/sweep str<strong>at</strong>egy. Here, eachmemory object needs only a single mark bit r<strong>at</strong>her than a reference counter field.When free store is exhausted, a separ<strong>at</strong>e garbage collection phase takes place asfollows.1. Clear all mark bits.2. Perform depth-first search (DFS) following pointers beginning with eachvariable on the system’s list of st<strong>at</strong>ic variables. Each memory element encounteredduring the DFS has its mark bit turned on.3. A “sweep” is made through the memory pool, visiting all elements. Unmarkedelements are considered garbage <strong>and</strong> placed in free store.The advantages of the mark/sweep approach are th<strong>at</strong> it needs less space than isnecessary for reference counts, <strong>and</strong> it works for cycles. However, there is a majordisadvantage. This is a “hidden” space requirement needed to do the processing.DFS is a recursive algorithm: Either it must be implemented recursively, in whichcase the compiler’s runtime system maintains a stack, or else the memory managercan maintain its own stack. Wh<strong>at</strong> happens if all memory is contained in a singlelinked list? Then the depth of the recursion (or the size of the stack) is the numberof memory cells! Unfortun<strong>at</strong>ely, the space for the DFS stack must be available <strong>at</strong>the worst conceivable time, th<strong>at</strong> is, when free memory has been exhausted.Fortun<strong>at</strong>ely, a clever technique allows DFS to be performed without requiringadditional space for a stack. Instead, the structure being traversed is used to holdthe stack. At each step deeper into the traversal, instead of storing a pointer on thestack, we “borrow” the pointer being followed. This pointer is set to point backto the node we just came from in the previous step, as illustr<strong>at</strong>ed by Figure 12.18.Each borrowed pointer stores an additional bit to tell us whether we came down

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!