Posted on: January 21, 2025 Posted by: rahulgite Comments: 0

Java provides several garbage collection (GC) algorithms, each designed to optimize memory management for specific application requirements. This document explores the major garbage collectors available in Java, their features, algorithms (detailed), use cases, configurations, and default settings in modern Java versions.


Key Concepts: Young Generation, Old Generation, Eden, Survivor, and Old Regions

1. Young Generation:

  • The memory space where new objects are allocated.
  • Composed of the Eden region and two Survivor regions.
  • Garbage collection in this space is referred to as Minor GC.
  • Most objects are short-lived and are quickly collected in this phase.

2. Old Generation:

  • The memory space where long-lived objects reside.
  • Objects promoted from the young generation are moved here.
  • Garbage collection in this space is referred to as Major GC or Full GC.
  • Collection occurs less frequently but takes longer as it involves compacting and cleaning large amounts of memory.

3. Eden Region:

  • Part of the young generation.
  • All new objects are initially allocated in the Eden space.
  • When the Eden region fills up, a Minor GC occurs to clean up unreachable objects.
  • Surviving objects are moved to the Survivor regions.

4. Survivor Regions:

  • Two spaces (S0 and S1) in the young generation that store objects that survive garbage collection in the Eden region.
  • Objects are promoted between S0 and S1 during multiple GC cycles.
  • After a predefined threshold (controlled by -XX:MaxTenuringThreshold), surviving objects are promoted to the Old generation.

5. Old Region:

  • Part of the old generation.
  • Stores objects that have survived multiple GC cycles in the young generation.
  • Garbage collection in this region uses Major GC, which may involve compacting memory to prevent fragmentation.

1. Serial Garbage Collector

Overview:

  • A single-threaded garbage collector.
  • Best suited for applications with small heaps (e.g., desktops, embedded systems).

Algorithm (Detailed):

  1. Minor GC:
    • Collects garbage in the young generation (Eden and Survivor spaces).
    • All threads are paused (stop-the-world).
  2. Major GC (Full GC):
    • Collects garbage in the old generation.
    • Pauses all threads to perform marking and sweeping.
    • Compacts memory to prevent fragmentation.

Features:

  • Simple and efficient for small heaps.
  • Minimal overhead since only one thread is used.

Default Status:

  • Not the default GC in any modern Java version.

Use Case:

  • Applications running in single-threaded environments or with small heaps.

Configuration:

java -XX:+UseSerialGC -Xms512m -Xmx1g MyApplication

2. Parallel Garbage Collector

Overview:

  • A multi-threaded collector designed for throughput optimization.
  • Executes GC tasks in parallel using multiple threads.

Algorithm (Detailed):

  1. Minor GC:
    • Uses multiple threads to clean up the young generation.
    • Pauses application threads (stop-the-world).
  2. Major GC (Full GC):
    • Cleans up the old generation using multiple threads.
    • Compacts memory to reduce fragmentation.
    • Uses parallel threads to minimize pause duration.

Features:

  • High throughput by minimizing GC overhead.
  • Efficient for applications with large heaps.

Default Status:

  • Default GC for Java 8 and earlier versions.

Use Case:

  • Batch processing or scientific applications where throughput is more important than latency.

Configuration:

java -XX:+UseParallelGC -Xms2g -Xmx4g MyApplication

3. CMS (Concurrent Mark-Sweep) Garbage Collector

Overview:

  • A low-latency garbage collector that performs most of its work concurrently with application threads.
  • Deprecated in Java 9 and removed in Java 14.

Algorithm (Detailed):

  1. Initial Marking:
    • Pauses application threads to identify root references.
  2. Concurrent Marking:
    • Identifies live objects concurrently with application threads.
  3. Remarking:
    • Pauses threads briefly to finalize markings.
  4. Concurrent Sweeping:
    • Frees memory occupied by unreachable objects without compacting memory.

Features:

  • Reduces pause times by performing marking and sweeping concurrently.
  • Can cause fragmentation since it does not compact memory.

Default Status:

  • Deprecated and removed, no longer a default in any Java version.

Use Case:

  • Legacy applications requiring low-latency garbage collection.

Configuration:

java -XX:+UseConcMarkSweepGC -Xms4g -Xmx8g MyApplication

4. G1 Garbage Collector (Garbage-First)

Overview:

  • Introduced in Java 7 and became the default GC in Java 9.
  • Balances throughput and low-latency requirements.

Algorithm (Detailed):

  1. Heap Division:
    • Divides the heap into equal-sized regions, including Eden, Survivor, and Old regions.
  2. Young Generation Collection:
    • Cleans up regions in the young generation.
    • Moves live objects to Survivor or Old regions.
  3. Concurrent Marking:
    • Identifies reachable objects across all regions.
  4. Mixed Collection:
    • Cleans up both young and old regions with the most garbage.
  5. Full GC:
    • Rarely triggered, performs a stop-the-world collection across all regions.

Features:

  • Predictable pause times using -XX:MaxGCPauseMillis.
  • Balances latency and throughput effectively.

Default Status:

  • Default GC from Java 9 onward (unless specified otherwise).

Use Case:

  • Applications requiring predictable latency and moderate throughput.

Configuration:

java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -Xmx4g MyApplication

5. Z Garbage Collector (ZGC)

Overview:

  • Introduced in Java 11 and became production-ready in Java 15.
  • Focuses on extremely low pause times (less than 10ms) and supports very large heaps (up to 16TB).

Algorithm (Detailed):

  1. Concurrent Marking:
    • Identifies live objects concurrently with application threads.
  2. Concurrent Relocation:
    • Moves live objects to compact memory regions while the application continues running.
  3. Pointer Coloring:
    • Uses bits in object pointers to track GC metadata, eliminating the need for stop-the-world pauses.
  4. Minimal Stop-the-World:
    • Only pauses application threads briefly for initialization and finalization.

Features:

  • Handles heaps up to 16TB.
  • Sub-10ms pause times regardless of heap size.
  • No full GCs.

Default Status:

  • Not a default GC but available as an option.

Use Case:

  • Real-time systems, trading platforms, or applications with massive heaps.

Configuration:

java -XX:+UseZGC -Xmx8g MyApplication

6. Shenandoah Garbage Collector

Overview:

  • Introduced in Java 12.
  • Optimized for low-pause times by performing concurrent compaction.

Algorithm (Detailed):

  1. Concurrent Marking:
    • Identifies live objects concurrently with application threads.
  2. Concurrent Evacuation:
    • Moves live objects to compact memory regions while application threads continue running.
  3. Minimal Stop-the-World:
    • Only pauses threads briefly for root reference analysis and finalization.
  4. Concurrent Cleanup:
    • Frees memory from unreachable objects without extended pauses.

Features:

  • Low-pause times by performing compaction concurrently.
  • Supports heaps up to 2TB.

Default Status:

  • Not a default GC but available as an option.

Use Case:

  • Applications requiring low-latency garbage collection but cannot use ZGC.

Configuration:

java -XX:+UseShenandoahGC -Xmx4g MyApplication

Comparison of Garbage Collectors

Garbage CollectorIntroduced InDefault StatusDeprecated StatusAlgorithm DetailsBest Use CaseHeap SizeLatencyThroughput
Serial GCJava 1.1Never defaultNot deprecatedStop-the-world sequentialSingle-threaded appsSmallHighLow
Parallel GCJava 1.4Default in Java 8-Not deprecatedParallel minor/full GCBatch processingMedium to LargeMediumHigh
CMS GCJava 1.4Removed in Java 14Deprecated in Java 9Concurrent mark-sweepLow-latency legacy appsMedium to LargeLowMedium
G1 GCJava 7Default in Java 9+Not deprecatedRegion-based, concurrentGeneral-purpose low-latencySmall to LargeLowModerate
ZGCJava 11Never defaultNot deprecatedFully concurrentUltra-low latency, huge heapsUp to 16TBVery LowModerate
Shenandoah GCJava 12Never defaultNot deprecatedConcurrent with compactionLow-latency, medium heapsUp to 2TBLowModerate

Explanation of Commands

  • -XX:+Use<GCName>: Enables a specific garbage collector (e.g., UseG1GC, UseZGC).
  • -Xms<size>: Sets the initial heap size for the JVM.
  • -Xmx<size>: Sets the maximum heap size for the JVM.
  • -XX:MaxGCPauseMillis=<time>: Sets a target maximum pause time for G1 GC.
  • -XX:InitiatingHeapOccupancyPercent=<value>: Specifies the heap usage percentage at which concurrent GC cycles start.

Choosing the Right GC

The best garbage collector depends on your application requirements:

  • Serial GC: Best for single-threaded applications or small heaps.
  • Parallel GC: Optimal for throughput-intensive batch processing.
  • G1 GC: A balanced choice for most modern applications.
  • ZGC: Ideal for ultra-low latency and massive heap sizes.
  • Shenandoah GC: A good alternative to ZGC for low-latency needs.

Latency vs Throughput

Latency:

  • Definition: The time it takes for a request to be processed and responded to.
  • Measured in: milliseconds (ms) or microseconds (µs).
  • Use Case: Critical in real-time systems (e.g., online trading, games).
  • Example: If you send a request to a server and it responds in 120ms, that’s the latency.

Throughput:

  • Definition: The number of requests or tasks a system can handle in a given time period.
  • Measured in: requests per second (RPS) or transactions per second (TPS).
  • Use Case: Important in batch processing or high-volume systems.
  • Example: A web server handling 10,000 requests per second has a throughput of 10k RPS.

Key Difference:

  • Latency is about speed of one task.
  • Throughput is about volume of tasks over time.

Analogy:

  • Latency = Time taken to deliver one pizza.
  • Throughput = Number of pizzas delivered in an hour.

Summary

Java’s garbage collectors cater to a wide range of application needs, from small, single-threaded programs to massive, real-time systems. The default GC in modern Java versions is G1 GC, but developers can choose alternatives like ZGC or Shenandoah based on specific requirements. Understanding the strengths, algorithms, and limitations of each collector helps optimize application performance and memory usage.

Leave a Comment