How does heap compaction work quickly?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Heap compaction sounds expensive because the runtime may move many live objects and update many references. It works quickly in practice because modern garbage collectors do not treat memory as an unstructured mystery: they know where objects are, where references can exist, and which parts of the heap actually need compaction.
Compacting Collectors Do Not Start From Scratch
A compacting garbage collector is usually part of a mark-and-compact or copying design. Before moving anything, it has already identified the live objects during marking or tracing.
That means compaction is not “search the whole process for pointers and hope.” The collector already has structured information such as:
- object boundaries
- object sizes
- exact reference locations in managed objects
- root sets from stacks, registers, and globals
Because of that metadata, pointer updates can be systematic and linear rather than guesswork.
The High-Level Flow
A simple compacting collector often does this:
- mark live objects
- compute each live object's new address
- update references to point to the new address
- copy or slide the live objects into their compacted positions
The heart of the speed is that these passes are mostly linear scans over memory and metadata. Linear memory access is cache-friendly and much faster than fragmented arbitrary work.
Forwarding Addresses Make Pointer Fixups Cheap
A common trick is to record a forwarding address for each live object. Once the collector decides the new compacted layout, every live object knows where it will end up.
Then reference updating becomes straightforward: when the collector finds a reference to an object, it replaces the old pointer with the forwarding address.
Here is a tiny Python model of the idea.
Real runtimes are much more sophisticated, but the principle is the same: decide destinations first, then fix references using a table or in-object forwarding metadata.
Only Live Objects Move
Compaction is fast partly because dead objects are ignored. The collector does not waste time preserving gaps that no longer matter. If a region has many dead objects, compaction may actually reduce future allocation cost dramatically by turning fragmented free space into a single contiguous area.
That is why compaction can be worthwhile even when it pauses the program for a while. The runtime pays a focused cleanup cost to make later allocations very cheap.
Generational Design Reduces the Scope
Modern collectors are often generational. Most objects die young, so the runtime concentrates frequent collection and compaction on the young generation, which is relatively small.
This is one of the biggest reasons compaction can feel fast:
- the collector usually is not compacting the whole heap every time
- many short-lived objects die before needing any movement at all
- surviving young objects are copied into a compact region efficiently
By restricting heavy work to the portion of the heap where it pays off most, the runtime keeps pause times lower.
Exact Metadata Avoids Blind Scanning
In managed runtimes, the collector often knows which fields of an object are references and which are plain values. It also knows how to enumerate roots from stack frames or generated metadata.
That exactness matters. If the collector had to scan every word of memory and guess whether it was a pointer, compaction would be far slower and far less reliable.
This is one of the fundamental advantages managed runtimes have over manual memory models for advanced GC strategies.
Parallel and Incremental Techniques Help Further
Modern runtimes also speed compaction with engineering tactics such as:
- parallel marking and copying across worker threads
- remembered sets and card tables to limit cross-generation scanning
- incremental or partially concurrent phases to spread out work
- bump-pointer allocation after compaction, which makes new allocations nearly trivial
Compaction is not fast because moving memory is free. It is fast because the runtime organizes the work so that movement is predictable, localized, and valuable.
Common Pitfalls
A common misunderstanding is thinking the collector must search the whole address space for every pointer update. Managed runtimes usually maintain enough metadata to avoid that.
Another mistake is assuming the entire heap is compacted on every collection. In practice, collectors often compact only selected regions or generations.
People also underestimate how much future allocation speed matters. Compaction is not only about reclaiming space; it is also about restoring cheap contiguous allocation.
Finally, do not confuse heap compaction with general OS-level memory defragmentation. The runtime is operating on managed heap structure it understands deeply.
Summary
- Heap compaction is fast because collectors already know object layout and reference locations.
- The work is mostly linear scans plus memory copying, which modern hardware handles well.
- Forwarding addresses make reference updates systematic.
- Generational collectors compact only the parts of the heap where it pays off most.
- Compaction improves future allocation speed by rebuilding contiguous free space.
Related reading
- How does Java implement hash tables?
- How does Kafka guarantee consumers doesn't read a single message twice?
- How does Kafka guarantee sequential disk access?
- How does one implement graph algorithms that require efficient contraction and expansion of connected components?
- How does Java Garbage Collection work with Circular References?
- How does Java makes use of multiple cores?
- How does one join string-type array-items, each with a comma character, except for the last item which has to be joined by and?
- How does rabbitmq heartbeat work

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.