Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

JVM Garbage Collector Implementations and Log Diagnostics

Tech 1

Selection of a garbage collection (GC) strategy in a Java Virtual Machine (JVM) typically involves pairing specific algorithms for the Young and Old generations. While default settings are provided based on the host environment, fine-tuning requires an understanding of the underlying mechanics of each collector. The following table summarizes the primary collector combinations available in modern JDKs:

Young Generation Old Generation JVM Argument
Serial Serial -XX:+UseSerialGC
Parallel Scavenge Serial -XX:+UseParallelGC -XX:-UseParallelOldGC
Paralel Scavenge Parallel Old -XX:+UseParallelGC -XX:+UseParallelOldGC
Parallel New CMS -XX:+UseParNewGC -XX:+UseConcMarkSweepGC
G1 (Unified) G1 (Unified) -XX:+UseG1GC

Serial Garbage Collector

The Serial GC is a single-threaded collector that utilizes a mark-copy algorithm for the Young generation and a mark-sweep-compact algorithm for the Old ganeration. Because it performs all work on a single thread, it triggers a Stop-The-World (STW) event where all application threads are paused.

To enable this collector:

java -XX:+UseSerialGC -Xmx512m MyApp

This collector is suitable for small-scale applications (heaps under several hundred MBs) or single-core environments. In a multi-core server environment, the Serial GC is inefficient as it fails to utilize available hardware resources during the pause.

Diagnostic Log Example (Minor GC)

2024-10-12T10:00:01.123+0800: 12.456: [GC (Allocation Failure) 12.456: [DefNew: 131072K->16384K(147456K), 0.0123456 secs] 458752K->393216K(507904K), 0.0125000 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
  1. DefNew: Indicates the use of the Serial Young generation collector.
  2. 131072K->16384K: Memory usage before and after collection in the Young generation.
  3. 458752K->393216K: Total heap usage before and after collection.
  4. real=0.01 secs: Total wall-clock time the application was paused.

Parallel Garbage Collector

Parallel GC (also known as the Throughput Collector) focuses on maximizing CPU cycles dedicated to application logic by using multiple threads during GC pauses. Like the Serial GC, it uses mark-copy in Young and mark-sweep-compact in Old, but the execution of these phases is parallelized.

Enablement commands:

java -XX:+UseParallelGC -XX:+UseParallelOldGC -Xmx4g MyApp

Diagnostic Log Example (Full GC)

2024-10-12T10:15:05.789+0800: 924.123: [Full GC (Ergonomics) [PSYoungGen: 524288K->0K(1048576K)] [ParOldGen: 2097152K->1572864K(3145728K)] 2621440K->1572864K(4194304K), [Metaspace: 32768K->32768K(1048576K)], 0.5432100 secs] [Times: user=3.20 sys=0.15, real=0.54 secs]
  1. PSYoungGen and ParOldGen: Identifiers for the Parallel Young and Old collectors.
  2. Ergonomics: The JVM decided to trigger GC to meet specified performance goals.
  3. user=3.20... real=0.54: The total CPU time is higher than the wall-clock time, confirming parallel execution across multiple cores.

Concurrent Mark and Sweep (CMS)

CMS was designed to reduce the duration of GC pauses in the Old generation. It accomplishes this by performing most of its work concurrently with the application threads. CMS does not compact the Old generation; instead, it maintains free-lists to manage memory, which can lead to fragmentation over time.

CMS operates in several distinct phases:

  1. Initial Mark (STW): Scans for objects directly reachable from GC roots.
  2. Concurrent Mark: Traverses the object graph from roots while the application runs.
  3. Concurrent Preclean: Identifies "dirty" cards (memory regions modified during marking).
  4. Concurrent Abortable Preclean: Attempts to minimize the subsequent STW pause.
  5. Final Remark (STW): Finalizes the marking of live objects, resolving changes made during concurrent phases.
  6. Concurrent Sweep: Reclaims memory from unreachable objects.
  7. Concurrent Reset: Clears internal structures for the next cycle.

Garbage First (G1)

G1 is a region-based collector that divides the heap into approximately 2048 equal-sized regions. Each region can act as Eden, Survivor, or Old space. This flexibility allows G1 to prioritize the collection of regions with the most garbage, hence the name "Garbage First."

Key features include:

  • Predictable Pauses: Users can set a target pause time using -XX:MaxGCPauseMillis=N.
  • Mixed Collections: Unlike other collectors, G1 can collect both Young and a subset of Old regions in a single cycle.
  • Compacsion: G1 avoids fragmentation by moving objects from collected regions into empty ones during pauses.

G1 Marking Cycle Phases:

  • Initial Mark: Triggered during a Young GC pause, marking roots.
  • Root Region Scan: Concurrent scanning of survivors for references into Old regions.
  • Concurrent Mark: Global marking of live objects across the heap.
  • Remark (STW): Finalizes marking and processes references.
  • Cleanup (STW/Concurrent): Identifies completely empty regions and prepares for the evacuation phase.

Shenandoah and ZGC

Modern JVMs include ultra-low pause collectors like Shenandoah and ZGC. These collectors aim for pause times consistently below 10ms, even on multi-terabyte heaps. They achieve this by performing evacuation (moving objects) concurrently with application threads using specialized techniques like Brooks pointers (Shenandoah) or Load Barriers and Colored Pointers (ZGC). These collectors are generally non-generational or have only recently added generational support to further optimize performance.

Tags: JVM

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.