Memory Management/Memory Compacting
Compacting
[edit | edit source]As a memory pool is allocated from, over time the pool will become fragmented. Some objects from the pool will be allocated surrounded by objects that are not, in seemingly random order. Memory compaction is the process of moving allocated objects together and leaving empty space together.
Consider a system with 3 pages and about 50% of their objects are allocated. By compacting, all the living objects are moved into the first two pages, leaving the second half of the second page and the third page completely empty. The empty page can then be ignored during future mark/sweep phases since it is known to be empty of living objects, or it can be released and returned to the desired operating system. Memory compacting is very important technique for allocating memory properly