SI 413 Fall 2021 / Homeworks


This is the archived website of SI 413 from the Fall 2021 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 10: Garbage

1 Reference Count Practice

Consider the following SPL code:

new f := lambda a {
  new g := lambda b { ret := b + b/2; };
  new h := lambda c {
    new x := a*c;
    ret := lambda d { ret := g@d < x; };
  };
  ret := h;
};
new foo := f@3@4;
write foo@8;
foo := 20;

Here are the frames and closures that exist just before the last line is executed. (Note: it would be good practice to see if you could recreate this diagram yourself!)

Frames and closures for ex 1 

  1. Using the labels of each frame above, indicate what the reference count for each frame is at this point in the program.

  2. Repeat (a), showing what happens to the reference counts after the last line in the program is executed.

2 Mark & Sweep Practice

For this problem, use the same example program and frames-and-closures diagram as for the first problem.

  1. Using the labels of each frame above, indicate which frames would be garbage collected at this point using the mark and sweep method.

  2. Repeat (a), showing what happens in mark and sweep after the last line in the program is executed.

3 Garbage 101

Write a short program in any programming language other than SPL that creates exactly 101 objects of garbage which cannot be collected using normal reference counting.

That is, your program should somehow cause at least 101 objects to be allocated on the heap, which are unreachable at the end of the program and therefore considered “garbage”, but which will not be collected by a naïve reference counting garbage collector implementation.

Clearly indicate the programming language you choose and try to keep it simple!