Garbage collection

Hand-drawn branching network of hatched terracotta ovals joined by thick strokes, with a few pale sand outlines nearby, over sand, umber and moss herringbone hatching.

Garbage collection automatically frees memory for reuse when a running program can no longer reach the data stored there (Wilson, 1992). In Java benchmarks, collection costs varied with the memory available, and shorter collection pauses sometimes came with slower application responses (Blackburn et al., 2025; Cai et al., 2022).

Tracing and reference counting

Tracing follows references from a set of roots. A reference is a link to a stored object, and roots are the starting references available to the running program. A mark-and-sweep collector marks every object it reaches, then reclaims the space occupied by unmarked objects (Wilson, 1992). John McCarthy’s 1960 account of Lisp described tracing storage from a set of registers that served as roots (McCarthy, 1960).

Reference counting keeps a tally of how many references point to each object. When the tally reaches zero, the collector can reclaim the object’s memory (Wilson, 1992). George Collins described using such counts in 1960 to reclaim storage shared among linked lists (Collins, 1960).

Suppose two objects refer to each other and the program drops its last reference to either of them. Each object’s count stays at one, so basic reference counting leaves both objects in memory. Both objects are unreachable from the roots of a tracing collector, which can reclaim them (Wilson, 1992).

Generations and concurrent collection

Generational collection groups objects by age so the collector can examine younger objects more often. Collecting a smaller region more frequently limits repeated work on objects that live a long time (Wilson, 1992). Henry Lieberman and Carl Hewitt proposed a design in 1983 that made storage for short-lived objects cheaper than storage for long-lived objects (Lieberman & Hewitt, 1983). The Generational ZGC design for the Java Virtual Machine also keeps younger and older objects in separate generations (Vidstedt, 2023).

Concurrent collection lets the program keep running during some of the collector’s work. Edsger Dijkstra and colleagues described a design in 1978 that ran most collection work on a separate processor while computation continued (Dijkstra et al., 1978).

Memory, execution time and responsiveness

The measured cost depends on the heap size and the basis for comparison. The heap is the memory where the program allocates objects. When less heap space was available, collection used more processor time in tests of five OpenJDK 21 collectors across 22 DaCapo Chopin benchmarks (Blackburn et al., 2025).

Benchmark comparisonBaselineResult
One generational collector in simulated Java runsSimulated explicit allocation and freeing, with objects freed when they became unreachableThe collector, which used a non-copying older generation, matched the baseline’s performance at five times the baseline’s memory (Hertz & Berger, 2005).
Five OpenJDK 17 collectors across 16 DaCapo benchmarksAn estimate of the time each application needed to do its own workEstimated minimum extra elapsed time ranged from 7% to 82% across collectors. The study combined each collector’s results across benchmarks using a geometric mean, a way to average ratios. Each benchmark ran with 2.4 times the smallest heap it could use with the G1 collector (Cai et al., 2022).

Application latency is the time between a request for work and the application’s response. A collection pause is time when the collector stops the application from running. In the OpenJDK 17 tests, collectors designed to shorten pauses sometimes produced longer application response times than collectors that stopped the application to collect (Cai et al., 2022).

Further reading

  • Wilson (1992) surveys tracing, reference counting and collection by generation.
  • Dijkstra et al. (1978) presents an early design for running collection alongside a program.
  • Cai et al. (2022) compares elapsed time and application latency across production Java collectors.

Known gaps

What this article cannot tell you yet.

  1. The performance comparisons cover selected Java workloads. The size of these trade-offs in other runtimes and in deployed services remains open.

References

Solid source
Rated highly trustworthy by the research, and every passage taken from it was found in it.
Weak source
Rated low, or a passage taken from it was not found in it.
Medium trust
Rated medium by the research.
Not checked
Rated high, but its passages could not all be checked against it.
Show all 9 references