March 24, 2020 /   ZGC   Java   Garbage Collection   JDK 11

Python Philosophy

ZGC is a low pause time or low latency Garbage collector designed to handle modern workloads where memory is not limited to GBs. In the cases, where heap size is in Terabytes it is important to keep pause times low when garbage collector runs. ZGC address the very issue. ZGC can handle up to 4TB of heap.

ZGC was introduced in JDK 11 with improvements in later versions. ZGC comes in 64-bit only, Will explain why in a bit.

Enabling ZGC

To enable use following JVM args

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC 

Goals of ZGC

ZGC is expected to ensure following

  1. low pause time below 10ms
  2. pause time does not vary with size of heap
  3. Work with large size heaps (Heaps in GBs or TBs)

How Garbage Collection works in ZGC?

ZGC uses the term Pointer Coloring that utilises the virtual memory address space using 64-bit addressing. Out of 64-bit, 42 bits are used to address actual memory and rest 22 bits are reserved for maintaining pointer state. Out of 22 reserved states, 4 consecutive bits are used only. These 4 bits are named as finalizable, remap, mark0 and mark1. These bits are set or unset in GC cycles.

Since there is some extra bits are set in the address, ZGC requires to decode or deference actual address from the coloured pointer. This technique is called Multi-mapping.

ZGC Cylces

There are two phases of in garbage collection - Marking and Relocation.

Marking

Marking phase is a stop-the-world phase where are live objects are marked that accessible through GC roots(local variables). Then there is a concurrent marking phase to mark all the objects that are accessible from the live set.

Relocation

This phase is also a two step phase, in first step GC reallocate objects directly accessible from local variables (GC roots) that are in relocation set. While relocating objects their references are also remapped.

This step is a stop-the-world phase but this would be a low pause as only a small set of live objects (Objects accessible from GC roots) are relocated in this phase.

In the next phase all the remaining live set is relocated concurrently.