12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 12.3 Memory Management 441<strong>and</strong> which parts are garbage. In particular, a list is kept of all program variables,<strong>and</strong> any memory locations not reachable from one of these variables are considered<strong>to</strong> be garbage. When the garbage collec<strong>to</strong>r executes, all unused memory locationsare placed in free s<strong>to</strong>re for future access. This approach has the advantage that itallows for easy collection of garbage. It has the disadvantage, from a user’s poin<strong>to</strong>f view, that every so often the system must halt while it performs garbage collection.For example, garbage collection is noticeable in the Emacs text edi<strong>to</strong>r, whichis normally implemented in LISP. Occasionally the user must wait for a momentwhile the memory management system performs garbage collection.The Java programming language also makes use of garbage collection. As inLISP, it is common practice in Java <strong>to</strong> allocate dynamic memory as needed, <strong>and</strong> <strong>to</strong>later drop all references <strong>to</strong> that memory. The garbage collec<strong>to</strong>r is responsible forreclaiming such unused space as necessary. This might require extra time whenrunning the program, but it makes life considerably easier for the programmer. Incontrast, many large applications written in C++ (even commonly used commercialsoftware) contain memory leaks that will in time cause the program <strong>to</strong> fail.Several algorithms have been used for garbage collection. One is the referencecount algorithm. Here, every dynamically allocated memory block includes spacefor a count field. Whenever a pointer is directed <strong>to</strong> a memory block, the referencecount is increased. Whenever a pointer is directed away from a memory block,the reference count is decreased. If the count ever becomes zero, then the memoryblock is considered garbage <strong>and</strong> is immediately placed in free s<strong>to</strong>re. This approachhas the advantage that it does not require an explicit garbage collection phase, becauseinformation is put in free s<strong>to</strong>re immediately when it becomes garbage.The reference count algorithm is used by the UNIX file system. Files can havemultiple names, called links. The file system keeps a count of the number of links <strong>to</strong>each file. Whenever a file is “deleted,” in actuality its link field is simply reduced byone. If there is another link <strong>to</strong> the file, then no space is recovered by the file system.Whenever the number of links goes <strong>to</strong> zero, the file’s space becomes available forreuse.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 a<strong>to</strong>m). Anothermajor problem occurs when garbage contains cycles. Consider Figure 12.17. Hereeach memory object is pointed <strong>to</strong> once, but the collection of objects is still garbagebecause no pointer points <strong>to</strong> the collection. Thus, reference counts only work when

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

Saved successfully!

Ooh no, something went wrong!