Compaction is a process in which the memory contents are shuffled so that all the occupied areas of memory is moved to one end of memory and all the free space is placed as a single large block.
Thus in compaction, a single large hole is created instead of number of small holes scattered throughout the memory.
Compaction makes contiguous free storage available which can be allocated to any requesting process.
The Compaction can be performed at different stages such as:
§ When any process terminates
§ When a new process cannot load due to fragmentation
§ At fixed intervals
§ When the user decides to perform it.
For example, if a block contains executable code, a branch instruction might be a pointer to another location in the same block. Compaction is performed in three phases.
First, the new location of each block is calculated to determine the distance the block will be moved.
Then each pointer is updated by adding to it the amount that the block it is pointing (in)to will be moved. Finally, the data is actually moved. There are various clever tricks possible to combine these operations.
Compaction is not always possible. If relocation is static and is done at assembly or load time, compaction cannot be done.
Compaction is possible only if relocation is dynamic and is done at execution time.