Z Garbage Collector (ZGC)
Java 11 has some great features, one is Z Garbage Collector (ZGC). The Z Garbage Collector, also known as ZGC, is a low latency scalable garbage collector designed to meet the following objectives.
- Pause times shall not exceed 10 ms
- Handle heaps ranging from a few hundred megabytes to multi terabytes in size
- Pause times do not increase with the size of the heap or live-set.
- Concurrent
- Region-based
- Compacting
- NUMA-aware: Non-uniform memory access (NUMA) is a way of configuring cluster of microprocessor into a multiprocessing system, so that memory can be shared locally and performance can be improved and the system's ability extended.
- Using colored pointers
- Using load barriers
In order to understand how ZGC fits into existing Garbage Collector, will first touch base the Garbage Collector Concepts.
ZGC uses two new techniques for Hotspot Garbage Collectors to achieve its goals: colored pointers and load barriers.
-
Pointer colouring
Pointer colouring is a technique which store metadata in unused bits of reference address.
It needs 42 bits for addressing (4TB).
4 bits for metadata as shown in below diagram.- Marked0
- Marked1
- Remapped
- Finalizable
One problem with pointer coloring is that when you need to dereference the pointer, it can create additional work, because you need to mask the bits of information. Many platforms such as SPARC have built-in pointer masking hardware support so it's not a concern but for x86, the ZGC team uses a simple multi-mapping technique.
- Multi-mapping : The operating system is responsible for maintaining mappings between virtual memory and physical memory, using a page table and the Memory Management Unit (MMU) and Translation Lookaside Buffer (TLB) of the processor, which translates the addresses needed by applications.
-
Load barriers
Load barriers are code that run when application thread loads a reference from the heap (i.e accesses a non-primitive field on an object)void getEmployee( Employee emp ) { String emp_name = emp.name; // would trigger the load barrier because we have loaded a reference from the heap System.out.println(emp_name); // no load barrier directly }
