CS 536 (F03) Homework 8: Garbage Collection

Due: November 24 in class (hardcopy) AND via turnin (assignment name hwk8).


Assignment Goals


Exercises

  1. Start with a final, compiled have-eye-color family tree program from homework 7. You may use your own version or my sample solution.

    You will need to make one small change as compared to the notes: any value on the stack that refers to a heap location should be stored in a box, since the garbage collector may need to change that address.

  2. Edit the program to include separate data areas for immediates and references as discussed in class.

  3. Implement two garbage collectors: mark & sweep, and stop & copy. The garbage collector should run whenever a memory allocation fails, so you need to modify the functions that allocate cells on the heap to check for memory availability.

    Submit two files, one for each garbage collector. Each file should define the following variables and functions so that I can evaluate your implementation:

    HEAP-SIZE : number number of cells in the heap
    have-color : symbol loc -> loc consumes a color and the location of a tree, and returns the location of the list of people with the given eye color
    print-list-at : loc -> void consumes the location of a list, and prints some representation of the list

    You will almost certainly want to use numbers for the loc type above, but you're allowed to choose some other representation.

    Finally, you should print a representation of the heap before and after every garbage collection. You can use the Scheme printf function:

       (printf "Before: Heap is ~s~n" Heap)
      
    Some final words of advice:

The required exercises stop here.


Advanced Option: Generational GC (to be discussed in the second GC lecture)

The advanced exercise is for students who want more of a challenge in implementing garbage collection. If you choose to do the advanced option, you do not need to implement mark & sweep or stop & copy.

  1. Start with a program update-eye-color that consumes a family tree, a person's name, and a new eye color and returns a new tree with the eye color on the named person changed to the given color. Convert this program to compiled form, including separate areas for immediates and references.

    Your compiler should include an operator set-eye-color! that takes an info and changes the value of the eye-color attribute (do NOT create a new info in the implementation of set-eye-color! -- structure mutation provides one of the challenges in generational GC).

  2. Add a generational garbage collector to this program. Follow the general instructions above for what features I want in your collector for testing purposes. Change the name and contract on your main function accordingly.

    HEAP-SIZE : number number of cells in the heap
    update-color : loc symbol symbol-> loc consumes the location of a tree, a name, and a color and returns the location of the updated tree
    print-tree-at : loc -> void consumes the location of a tree, and prints some representation of the tree

    See the "final words of advice" in the standard assignment for some helpful hints.

    Print a representation of the heap (your generations) before and after every garbage collection. The printed representation should clearly demarcate the generations. You can use the Scheme printf function:

       (printf "Before: Heap is ~s~n" Heap)
      


Back to the Assignments page