After its review concluded, JEP 423, Region Pinning for G1, has been Integrated into JDK 22. This JEP proposes to reduce GC latency by implementing region pinning for the G1 garbage collector. This will extend G1 so that arbitrary regions may be pinned during major and minor collection operations, and disabling the garbage collection process while implementing JNI critical regions may be avoided.
JEP 423 addresses a longstanding challenge to Java's interoperability with unmanaged languages such as C and C++. JNI, a critical component for this interoperability, allows Java applications to call and be called by native applications and libraries written in other languages. It provides functions to get and release direct pointers to Java objects, which must be used in pairs. The code executed between obtaining and releasing these pointers is considered a critical region, during which the JVM must ensure that the GC does not move the associated Java object. Traditionally, the default GC, G1, would disable garbage collection entirely during these critical regions, leading to significant latency issues. This approach often resulted in thread stalling, unnecessary out-of-memory conditions, and, in extreme cases, premature VM shutdowns.
The introduction of region pinning in G1 fundamentally changes this approach. Instead of disabling GC, JEP 423 allows G1 to pin specific memory regions containing critical objects during GC operations. This is achieved by maintaining a count of critical objects in each region, incrementing it when a critical object is obtained, and decrementing it when released. When the count is non-zero, the region is considered pinned and is not evacuated during GC operations. This ensures that critical objects are not moved, eliminating the need to disable GC and reducing latency significantly.
This new feature is a significant advancement in Java's garbage collection mechanism. It aims to resolve latency problems and guarantee no setbacks in GC pause times when no active JNI critical regions exist. Even when such regions are active, the regressions are minimal. The implementation of region pinning during both minor and major GC operations demonstrates the robustness of this solution.
However, JEP 423 also acknowledges potential risks and assumptions. One significant risk is the possibility of heap exhaustion if an application pins many regions simultaneously. While there's no direct solution to this, experiences with the Shenandoah GC, which already implements a similar mechanism, suggest that this risk is manageable.
In conclusion, JEP 423 introduces a significant technical enhancement to the G1 garbage collector within OpenJDK. By implementing region pinning, this update addresses the latency issues associated with Java Native Interface (JNI) critical regions, thereby improving the JVM's garbage collection handling during interactions with unmanaged languages. This development reflects OpenJDK's ongoing efforts to refine and optimize Java's performance and interoperability capabilities.