While I was working through the chapter 5.3 of SICP, I created small visualization of the stop-and-copy garbage collection algorithm.

I start with the following memory structure example.

The content of the root register is a pointer p4 to the list of registers (x y z). The register x points to address 6 where improper list (1 . 2) saved. The register y points to address 8 where list (x x) starts. Finally, the register z points to address 10 where list (3 4 5) starts. Addresses 1, 3, 5, 9, 11, 13, 15 contain garbage.

After we ran the GC algorithm, we got the following memory structure.

root now points to address 0, x to 1, y to 3, and z to 6.

Here is the visualization

References

  • Bigger picture on GitHub.
  • Animation frames for better view on GitHub.