Lecture 017

Automatic Memory Management

Storage bugs are hard to catch because the effect is far in time.

Garbage Collection was first introduced in Java in 1990 (although studied for Lisp in 1950s). It is very recent.

Automatic memory management is great but it reduces programmer's control. So if you want speed, better manage it yourself.

Memory leak is still possible (even likely when you forget to set a pointer that point to a root of large chunk of memory to NULL)

Mark and Sweep

Reachability: object x is reachable if and only if

If an object is reachable, doesn't mean it will be used.

Algorithm:

  1. from stack pointer (and accumulator or other registers), recursively mark memory region as reachable
  2. roots: the starting point of the trace
  3. garbage collect everything not marked reachable

If we garbage collect only when memory is full, then we have a problem since garbage collection algorithm itself may use memory.

Solution:

Garbage collection must work with constant space.

Stop and Copy

Algorithm: like a frame buffer

  1. divide heap into half, one called "old space" and the other called "new space"
  2. new memory is always allocated in "old space"
  3. when garbage collect, copy everything non-garbale from "old space" to "new space" by first copy and then recursively discover new non-garbale
  4. for the non-garbale memory in "old space", replace memory region to a "forwarding pointer" point to memory in "new space"
  5. swap role between "old space" and "new space"

This method provide less segmentation and is believed to be the fastest GC technique. Collection is relatively cheap since we only touch reachable objects.

Step 1

Step 1

Step 2

Step 2

Step 3

Step 3

Step 4

Step 4

Some language do not allow copying (C and C++). C and C++ have to use mark and sweep.

Conservative Collection

How to we tell if a memory region is a pointer? Well, we can't tell. But we can be conservative and keep everything that looks like a pointer.

Reference Counting

Technique:

  1. when creating object or creating reference, we store the number of reference to that object
  2. if the number of reference drop to zero, we garbage collect it

Advantage

Disadvantage:

More advanced garbage collection

Features:

Table of Content