Top About this closure stuff...Garbage Collection

Garbage Collection

  1. In writing the various phases of the compiler project, you will find yourself spending a good bit of time making sure you "free" memory areas that were no longer in use:

  2. If we were writing compilers in Java, this just would not be an issue.

  3. This approach to dynamic memory management has been standard in applicative languages (LISP, ML, Miranda, Haskell, etc.) for years (forever?). It makes programming simpler, safer, but possibly less efficient.

  4. Garbage collection is the process of automatically reclaiming dynamically allocated memory areas.

  5. The area in which dynamically allocated memory resides is called the "heap".

  6. Before we worry about how to automatically deallocate space from a heap, lets talk about some simple ways to organize a heap and allocate space as needed.
    Free lists
    Have allocated and available areas of memory intermixed with the unallocated units linked together on a "free list".
    Free/used regions
    Maintain a register/variable pointing to the moving boundary between the allocated and unallocated regions of memory. Note: In this scheme the "allocated" region will consist of intermixed used and un-used (i.e. garbage) words.

  7. The "free list" idea works best if all the units of memory allocated are of the same size. This was true in the first significant language whose implementation relied heavily on garbage collection, LISP.

  8. A dynamically allocated data structure in the heap can be safely disposed and its memory space reused only if it can no longer be accessed. This is true when no variable or method parameter ( or hidden temporary) or other accessible dynamically allocated object refers to it. Thus, to find the garbage we must find the stuff that can't be found!

  9. The next question is what to do when we find things. Two common answers:
    Mark-scan
    Just set a bit to mark the items as "found" as suggested in the code above. Then later make a pass through the whole heap and identify anything that isn't marked as "unfindable" stuff that can be reused.
    Copying
    Move everything found to a new, unused heap space (and update all pointers to refer to the new copies). When this is all done, the new heap will have used and free space but no garbage.

  10. Mark-scan garbage collection was developed for the implementation of LISP where all objects were of the same size. So, as the heap was scanned for free areas they could just be added to the free list.

  11. So, to make this all work, we have to plan the layout of data in memory in a way that will let us:

  12. The simplest approach to most of these requirement is to "tag" each object and/or pointer with a "descriptor" (sometimes one bit is enough) that will tell the garbage collector what it is.

Computer Science 434
Department of Computer Science
Williams College

Top About this closure stuff...Garbage Collection