5.4 Sweeping

With handles, we can “sweep” all the free space and avoid memory fragmentation. This is done by physicall moving all the dynamically allocated objects so that they are contiguous to each other. As an object is moved, its entry in the universal array of pointers is also updated.

In terms of implementation, a min-heap can be used to maintain pointers to all the dynamically allocated objects. This way, we can remove items from the heap (as the root), move the item to the next lowest available location and repeat the process until the heap is empty.