Shenandoah GC in JDK

Shenandoah's development began as an experimental project to provide an alternative to other garbage collectors that prioritize throughput or memory size over responsiveness. By version 12 of the JDK, Shenandoah was ready for use, although it remained marked as an experimental feature. This state was maintained to match the status of other garbage collectors such as Epsilon GC and ZGC.

The main event in the history of Shenandoah is its inclusion in the product features starting with version 15 of the JDK. That is, to use it, it was no longer necessary to unlock the experimental VM options. This change was mainly cosmetic and related to the classification of Shenandoah settings. However, it was a significant step forward and demonstrates the maturity of this garbage collector.

Shenandoah GC Operation Phases

Major phases of Shenandoah GC's operation include:

Initiating marking: runs concurrent marking, prepares the heap and application threads for concurrent marking, and then scans the root set. This is the first pause in the loop, and the main time consumer is scanning the root set.

Competitive marking: walks through the heap and keeps track of reachable objects. The phase runs in parallel with the application, and its duration depends on the number of living objects and the structure of the object graph in the heap.

Final marking: completes concurrent marking by emptying all pending mark/update queues and rescanning the root set. Also initiates evacuation by identifying regions for evacuation.

Competitive cleaning: restores regions of the heap that are completely free of living objects discovered after competitive labeling.

Competitive evacuation: copies objects from a collection set to other regions. This phase also runs in parallel with the application and its duration depends on the size of the selected collection set for the loop.

Initiating link updates: initiates the link update phase, preparing the GC for the next phase. This is the third pause in the cycle, the shortest of all.

Competitive link update: walks through the heap and updates references to objects that were moved during a competitive evacuation. The phase runs in parallel with the application.

Final link update: completes the link update phase by re-updating the existing root set. Also recycles regions from the collection set.

Competitive cleaning: Restores collection set regions that are now unlinked.

Setting up Shenandoah GC in JVM

Basic settings for Shenandoah GC in the JVM:

-XX:+UseShenandoahGC: option enables use of Shenandoah GC. Usage example: java -XX:+UseShenandoahGC MyApplication.

-XX:ShenandoahGCHeuristics: drives Shenandoah heuristics. For example, aggressive to reduce pause times by increasing CPU and memory usage. Usage example: java -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive MyApplication.

-XX:ShenandoahFreeThreshold: specifies the percentage of free memory on the heap after garbage collection. Usage example: java -XX:+UseShenandoahGC -XX:ShenandoahFreeThreshold=20 MyApplication.

-XX:ShenandoahAllocationThreshold: sets the threshold for starting garbage collection based on the percentage of occupied memory on the heap. Usage example: java -XX:+UseShenandoahGC -XX:ShenandoahAllocationThreshold=60 MyApplication.

-XX:ShenandoahGarbageThreshold: determines the percentage garbage in the heap, upon reaching which garbage collection will begin. Usage example: java -XX:+UseShenandoahGC -XX:ShenandoahGarbageThreshold=20 MyApplication.

ShenandoahUncommitDelay: The parameter specifies the delay (in ms) before the Shenandoah GC frees memory that has been allocated to the heap but is not currently in use. The default value for this parameter is 1000 ms. Usage example: java -XX:+UseShenandoahGC -XX:ShenandoahUncommitDelay=500 MyApplication (sets the delay to 500ms).

ShenandoahGCMode: This parameter allows you to select the Shenandoah GC operating mode. Possible values ​​include:

normal: Standard operating mode.

iu: (Immediate Update) mode in which links are updated immediately after the object is copied.

satb: (Snapshot At The Beginning) mode that uses an algorithm to snapshot the state of the heap at the beginning of the marking phase.

Running the collector

Adding a parameter -XX:+UseShenandoahGC to the command line when starting the application:

java -XX:+UseShenandoahGC -jar my-application.jar

Let's configure the collector like this:

java -XX:+UseShenandoahGC \
     -XX:+UnlockExperimentalVMOptions \
     -XX:ShenandoahGCHeuristics=adaptive \
     -XX:+AlwaysPreTouch \
     -Xms4G -Xmx4G \
     -jar my-application.jar

-XX:+UseShenandoahGC: includes use of Shenandoah GC.

-XX:+UnlockExperimentalVMOptions: unlocks experimental VM options

-XX:ShenandoahGCHeuristics=adaptive: Sets Shenandoah's heuristics to “adaptive”, which allows the garbage collector to automatically adapt to different application operating conditions.

-XX:+AlwaysPreTouch: causes the JVM to pre-allocate and initialize all heap memory at startup.

-Xms4G -Xmx4G: sets the initial and maximum heap size to 4 gigabytes.

Comparison with other collectors

Parameter / Collector

Shenandoah

G1

Parallel GC

Serial GC

CMS

Algorithm

Separation of generations, evacuation of areas

Separation of generations, evacuation of areas

Stop-the-world

Stop-the-world

Parallel mark, parallel clear

Pauses

Short, independent of heap size

Short, can grow with heap size

Relatively short, depends on heap size

Long, depends on heap size

Short, independent of heap size

Performance

High, especially with large piles

Good, but may deteriorate with large piles

High, but garbage collection fails

Low, mainly used for small applications

Good, but there may be delays due to garbage collection

Application

Suitable for large memory applications with low latency requirements

Suitable for large memory applications with moderate latency requirements

Good for multi-threaded applications with large amounts of memory

Suitable for simple or single threaded applications

Suitable for applications with low pause requirements


The Shenandoah garbage collector is suitable for applications where short garbage collection pauses are important, regardless of heap size.

Experts from OTUS discuss more practical Java development tools in a practical online course. You can find out more about the course link.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *