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 23:35, 17 August 2010. 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 added to a break table of relocation information. The break table is stored in heap areas that are marked as unused. 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: this will require the break table to be sorted. 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)