Apex › Systems & OS

How a copying collector compacts the heap (Cheney)

The other kind of tracing GC: instead of freeing dead objects in place (and leaving fragmentation), a copying collector splits the heap into two halves and copies only the live objects into the empty half, compacted, then flips. Cheney’s algorithm (1970) does the copy as a breadth-first walk using to-space itself as the queue — two pointers (scan, free) and a forwarding pointer in each moved object so nothing copies twice. Verified in node: the copied set equals the reachable set from the roots (400 heaps, 0 mismatch), forwarding pointers valid, to-space compact. Step it and watch the live objects relocate while the garbage is abandoned.

More in Systems & OS