Jump to content

Mark–compact algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jkl (talk | contribs) at 07:53, 18 August 2010 (Algorithms). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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, 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 references to the moved objects. This is addressed 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.

For efficiency, it is important to avoid relocating the break table as much as possible. However, the algorithm sometimes needs to move the break table out of the way to relocate live blocks, which may cause the relocation records to be jumbled (called rolling the table by the authors): this requires the break table to be sorted after the compaction is complete. Finally, the break table relocation records are used to adjust pointer fields.

References

  1. ^ "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)