Mark–compact algorithm
In computer science, a mark-compact algorithm is a type of garbage collection algorithm used to reclaim unreachable memory. Mark-compact algorithms can be regarded as a combination of the mark-sweep algorithm and Cheney's copying algorithm. First, reachable objects are marked, then a compacting step relocates the reachable (marked) objects towards the beginning of the heap area. Compacting garbage collection is used by Microsoft's Common Language Runtime and by the Glasgow Haskell Compiler.
Algorithms
After marking the live objects in the heap in the same fashion as the mark-sweep algorithm, the heap will often be fragmented. The goal of mark-compact algorithms is to shift the live objects in memory together so the fragmentation is eliminated. The challenge is to correctly update all pointers to the moved objects, which after the compaction have new addresses. The issue of handling pointer updates is handled in different ways.
Table-based compaction

A table-based algorithm was first described by Haddon and Waite in 1967[1]. It preserves the relative placement of the live blocks in the heap, and requires only a constant amount of overhead.
Compaction proceeds from the bottom of the heap (low addresses) to the top (high addresses). As live blocks are encountered, they are moved to the first available low address, and a record is appended to a break table of relocation information. For each live object, a record in the break table consists of the object's original address before the compaction and the difference between the original address and the new address after compaction. The break table is stored in the heap that is being compacted, but in an area that are marked as unused.
As compaction progresses, relocated blocks are copied towards the bottom of the heap. Eventually a block will need to be copied to the space occupied by the break table, which now must be relocated elsewhere. These movements of the break table, (called rolling the table by the authors) cause the relocation records to become disordered, requiring the break table to be sorted after the compaction is complete.
Finally, the break table relocation records are used to adjust pointer fields inside the blocks. The live blocks are examined for pointers, which can be looked up in the break table of size n in O(log n) time if the break table is sorted, for a total running time of O(n log n). Pointers are then adjusted by the amount specified in the relocation table.
References
- ^ "A compaction procedure for variable length storage elements". Computer Journal. 10: 162–165. 1967.
{{cite journal}}
: Unknown parameter|authors=
ignored (help); Unknown parameter|month=
ignored (help)