List/Sublist Projects:    Garbage Collector

((to remind you of the context of all this.....))

These projects revolve around the program for reading and printing lists and sublists, as discussed in class.

       Basic source for program is in This directory (.java and .zip form)

(Note: this incudes an updated Tokenizer -- please use the new copy).
(Note: requires Java 1.5 at least)

Overview:

Due dates:

  1. #13:  phase 1, week of Nov 26
  2. #15:  phase 2, Wednesday Dec 5
  3. #16   phase 3, or #17, eval pt 2:  Wednesday Dec 12
  4. Final date for any homework:  Friday, Dec 14
    After Friday, you should be concentrating on exams for this and your other classes.  No homework will be accepted after Friday, Dec 14, 2007.
  5. Final exam, Tuesday Dec. 18, 3-6 pm, M-1-619

This page documents the garbage collector project.  Alternatively, you can work on the augmented eval project (#16).


 Assignment  16: Garbage Collector for Item/List

In this phase, we pull the pieces together and implement a simple "garbage collector" for the List/Sub list world.  Most of the work has already been done!  We'll use a "mark and sweep" algorithm.

Provide a "status" field to Token, if you've not already done so.

Getting Organized:

 Start the garbage collector by visiting all the Tokens and setting their status to "unknown".
(This is why we'd like to have an array of all existing Tokens, to make it easy to find them)

Mark:

 we'll want to find all the Tokens which might possibly be in use.  For the basic program, that's the Item (probably an IList) currently under construction, anchored in a variable called "theList" in class IList.
Recursively visit all the Items belonging to theList, and whenever you find a Token, mark it -- by setting its "status" field to "inUse".
(Note that some Tokens, such as those for "(" and ")", never actually make it into theList.)

If your program has any other place where Tokens might be attached, you'll want to visit that also.  (Notice that the readList function carefully attaches new sublists to the parent before adding elements, so that theList always provides a path to everything we've built so far.)

Sweep:

Now sweep through the array of all Tokens.  Any one whose "status" is still "unknown" must actually be free -- as there is no existing path leading to it.  Change its status to "free" and add it to the freeList.

You're done.

Commentary:

Java uses a system very much like this to take care of free memory.  There is an initial pool.  Requests for "new" items are satisfied by taking memory from the pool.  When the pool is exhausted, the garbage collector marks all currently accessible objects, and then sweeps through the free area reclaiming anything that's not reachable.