Sitemap
Mika Tal

Mika Tal

Garbage collection in Java

3 min readMar 23, 2022

The purpose of the GC is to free heap memory from objects that are no longer needed (instead of us having to do this manually). The JVM can then use the free memory to allocate new objects.

But even if much space is available in total, a new allocation may not be feasible due to memory fragmentation. So the GC will need to perform compaction as well.

How does this work? We can get some information by looking at output from VisualVM with the plugin VisualGC.

The heap gets split to parts (spaces), and this makes the GC’s work a lot easier. But remember, we can’t just move objects around while the app is running. So we have to take a break (“stop-the-world pause”) from our app’s logic to let the GC run. And so, we want these breaks, taken to shift and clear up memory, to be as short as possible.

Press enter or click to view image in full size

The spaces

The spaces are: the young generation (eden, s0, s1), old generation and metaspace (metadata about classes).

This division is based on the assumption that most young objects are short lived, and that the longer an object survives (measured by the number of GC cycles) the more likely that it will still be needed.

Get Mika Tal’s stories in your inbox

Join Medium for free to get updates from this writer.

Using this hypothesis, the old generation will be checked only when doing a full GC, and it is preferred to scan the young generation more for garbage (minor GC).

An object’s life

An object is first created in the eden space. Objects in the eden space that survive a garbage collection are copied into one of the survivor spaces, s0 and s1 (only one survivor space is used at any time). This should be fast because there are only few surviving objects by assumption. So after a minor GC, we expect the eden space to be cleared.

Once an object survives a number of garbage collections, it is promoted to the old generation. A full GC will occur if the occupancy in the old generation is too high, or when there isn’t a large enough contiguous space to allocate an object. After a full GC, we expect to see a clear drop in the used heap.

If we dedicate more heap space to the young generation at the expense of the old, while keeping other factors constant, then minor GCs will occur less often (but will take more time each). On the other hand, there will be less memory for the old generation, so we may need more full GCs.

These parameters can be tuned using flags. For example -XX:NewRatio=N sets the ratio of old to new (a ratio of 2, for example, means the size of the old generation is twice as large), and -Xmx<size> sets maximum heap size.

By default, the JVM auto-tunes the sizes of the heap and the generations (adaptive sizing), so we don’t necessarily have to worry about it.

Types of collectors

From JDK 11, the default collector for multi-processor machines is the Garbage-First garbage collector (G1GC). With this collector, the heap is divided into smaller regions (roughly 2,048): eden regions, survivor regions and old regions. Regions with the least live data (and the most garbage) are collected first.

Other collectors available include: the serial collector, which uses a single thread (the default for single processor machines), the parallel collector (default in JDK 8), the deprecated concurrent mark-sweep collector (CMS), and other experimental garbage collectors are available as well.

--

--