Parallel Garbage Collector (GC)

The Parallel Garbage Collector (GC) or Throughput Collector is the default garbage collector used by JVM. The parallel garbage collector works the same as the serial garbage collector. The only difference between serial and parallel garbage collectors is that serial garbage collector uses a single thread for the garbage collection process while parallel garbage collector uses multiple threads for the garbage collection. Hence it is faster than Serial Garbage Collector.

Main Operation: 

Parallel Multi-thread to perform Garbage Collection (GC)

How does Parallel GC work?

  1. Initial Mark: The algorithm identifies and marks all objects directly referenced by the application’s active threads. This marking process is performed while the application is paused briefly.
  2. Concurrent Marking: While the application runs, the algorithm concurrently marks reachable objects in the memory, utilizing multiple threads. This concurrent marking phase reduces the overall pause time required for garbage collection.
  3. Remark: After the concurrent marking phase, the algorithm allows the application to continue running while it performs a final marking process to account for objects that may have become reachable during the concurrent phase.
  4. Concurrent Sweep and Compact: The Parallel GC concurrently sweeps and deallocates memory regions occupied by unreferenced objects. Simultaneously, it compacts the memory, moving live objects together to create contiguous free memory space. This concurrent sweep and compaction phase helps reduce the pause time further.
  5. Free Memory: After compaction, the algorithm updates the free memory space pointer to indicate the new location for object allocation.
Parallel Garbage Collector (GC)

Used For:

  • Parallel GC is suitable for multi-threaded applications or large-scale applications.
  • If your application has high transactional throughput requirements and can tolerate long, occasional pauses for garbage collection, Parallel GC can be a suitable choice. It focuses on maximizing throughput by allowing garbage collection to occur concurrently with application execution.
  • Applications that involve batch processing or data analysis tasks can benefit from Parallel GC. These types of applications often perform extensive computations and Parallel GC helps minimize the impact of garbage collection on overall processing time.
  • Parallel GC is well-suited for applications with moderate to large heap sizes. If your application requires a substantial heap to accommodate its memory needs, Parallel GC can efficiently manage memory and reduce the impact of garbage collection pauses.

Advantage:

  • Parallel GC has the features to control Pause Time, Throughput & Memory Footprint.
  • It improves the throughput by utilizing the processing power of multi-core processors.
  • Smaller pause time is one of the key advantages of Parallel GC. With the help of parallelizing the marking, sweeping, and compaction phases, the Parallel GC minimizes the pause times required for garbage collection, resulting in improved application responsiveness.
  • The algorithm’s ability to utilize multiple threads makes it highly scalable, allowing it to handle larger heaps and memory-intensive applications more efficiently.
  • Parallel GC performs well in scenarios where the application generates a significant amount of garbage.
  • Both minor and major collections are executed in parallel to further reduce garbage collection overhead.

Disadvantage:

  • It freezes all other running threads of the application until garbage collection operations have concluded i.e. stop the world event happens.
  • The Parallel GC algorithm utilizes multiple threads, leading to higher CPU utilization.
  • If GC is not tuned efficiently then it leads to longer pause time which will directly impact the application’s responsiveness.
  • It is not suitable for small-scale applications with limited resources.

Implementation: 

-XX: +UseParallelGC

Tuning Parallel GC:

  • -XX:MaxGCPauseMillis: This provides an input to the GC that pause times (in milliseconds) should be less than the provided amount. There is no default value for this flag. ‘Pause Time’ means the gap between two GC events. If a pause time is specified, the heap size and other parameters related to garbage collection are adjusted in an attempt to keep garbage collection pauses shorter than the specified value. These adjustments may cause the garbage collector to reduce the overall throughput of the application, and the desired pause time goal cannot always be met.
  • -XX:GCTimeRatio: This sets the ratio for the time spent between performing GC work and application work. The default value is set at 1 to 99. Refer to the article for more information: Throughput in Garbage Collection. You can also use the GC Time Ratio Value Calculator to get the correct value of the GCTimeRation argument.
  • -XX:+UseStringDeduplication: In JDK 18, String deduplication was ported from the G1 GC to Parallel GC. Enabling string deduplication can reduce memory footprint by about 10% but could impact pause times.
  • -XX:+ParallelGCThreads: This sets the number of threads that can be used for performing GC work. The default value is based on HotSpot ergonomics determined from available system resources.

Conclusion

Parallel GC garbage collector algorithm makes use of parallelism to improve garbage collection efficiency. It delivers higher performance, shorter pause times, and better scalability for memory-intensive workloads by employing many threads.


You may be interested: