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.

Sec. 12.3 Memory Management 423AaBcdeCfghFreelistFigure 12.16 Example of LISP list variables, including the system freelist.point of view, th<strong>at</strong> every so often the system must halt while it performs garbagecollection. For example, garbage collection is noticeable in the Emacs text editor,which is normally implemented in LISP. Occasionally the user must wait for amoment while 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 to alloc<strong>at</strong>e dynamic memory as needed, <strong>and</strong> tol<strong>at</strong>er drop all references to th<strong>at</strong> memory. The garbage collector 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 applic<strong>at</strong>ions written in C++ (even commonly used commercialsoftware) contain memory leaks th<strong>at</strong> will in time cause the program to fail.Several algorithms have been used for garbage collection. One is the referencecount algorithm. Here, every dynamically alloc<strong>at</strong>ed memory block includes spacefor a count field. Whenever a pointer is directed to 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 immedi<strong>at</strong>ely placed in free store. This approachhas the advantage th<strong>at</strong> it does not require an explicit garbage collection phase, becauseinform<strong>at</strong>ion is put in free store immedi<strong>at</strong>ely when it becomes garbage.Reference counts are used by the UNIX file system. Files can have multiplenames, called links. The file system keeps a count of the number of links to eachfile. Whenever a file is “deleted,” in actuality its link field is simply reduced byone. If there is another link to the file, then no space is recovered by the file system.When the number of links goes to zero, the file’s space becomes available for reuse.

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

Saved successfully!

Ooh no, something went wrong!