DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management
  • How Java Apps Litter Beyond the Heap
  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management

Trending

  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Mastering Advanced Aggregations in Spark SQL
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Streamlining Event Data in Event-Driven Ansible
  1. DZone
  2. Coding
  3. Java
  4. Different Garbage Collectors in Java: Exploring the Options

Different Garbage Collectors in Java: Exploring the Options

Why are there so many Java GC algorithms? Learn about their strengths, weaknesses, and how to choose the right one for optimal performance.

By 
Jill Thornhill user avatar
Jill Thornhill
·
Mar. 13, 25 · Analysis
Likes (7)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

Garbage collection in Java is something that just happens: you don’t have to worry about memory management. Or do you?

The garbage collector (GC) runs in the background, quietly doing its work. But this process can have a huge impact on performance. Understanding the concepts of advanced Java GC is invaluable in tuning and troubleshooting applications.

There are seven types of Java Garbage Collectors available in the JVM, some of which are obsolete. This article will look at the details, and compare the strengths and weaknesses of each. It will also look briefly at how you would go about evaluating garbage collection performance.

GC Evaluation Criteria

GC performance is evaluated on two criteria:

  • Throughput – Calculated as the percentage of time an application spends on actual work as opposed to time spent on GC;
  • Latency – The time during which the GC pauses the application. You should look at average latency times and maximum latency times when you’re evaluating performance.

Comparison of Types of Java Garbage Collectors

The table below gives details of the seven different algorithms, including the Java version where they were introduced, and the versions, if any, that use the algorithm as the default.

algorithm comments introduced used as default in

Serial GC

Original GC algorithm: single-threaded; tends to have long GC pauses; now obsolete

Java 1.2

Java 1.2 to Java 4; also in Java 5 to 8 single-core versions

Parallel GC

Multi-threaded; Distributed amongst cores; high throughput but long pauses

Java 5

Java 5 to 8 in multi-core versions

CMS GC (Concurrent Mark & Sweep)

Most work done concurrently; minimizes pauses; no compaction, therefore occasional long pauses for full GC

Java 4; deprecated in Java 9 and removed in Java 14

None

G1 GC (Garbage first)

Heap is divided into equal-sized regions. Mostly concurrent. Balances between latency and throughput; best for heap size < 32GB

Java 7

Java 8 and above

Shenandoah GC

Recommended for heap sizes  >32GB; has high CPU consumption

Open JDK 8 and above; Oracle JDK in JDK 11

None

ZGC

Recommended for heap size >32GB; prone to stalls on versions < Java 21

Java 11

None

Epsilon GC

This is a do-nothing GC, used only for benchmarking applications with and without GC

Java 11

None


G1 GC is probably the best algorithm in most cases, unless you have a very large heap (32GB or more). If this is the case, you can use Shenandoah if it is available or ZGC if you’re using Java 21 or later. ZGC can be unstable in earlier versions. Shenandoah may not be stable in the first few Oracle releases.

CMS was deprecated from Java 9 onwards because it didn’t deal well with compacting the heap. Fragmentation over time degraded performance, and resulted in long GC pauses for compaction.

Setting and Tuning the Garbage Collector

The table below shows the types of Java Garbage collectors, along with the JVM switches you’d use to set each of the GC algorithms for an application. It also contains links to tuning guides for each algorithm.

algorithm jvm switch to set it how to tune it: links

Serial GC

-XX:+UseSerialGC

Tuning Serial GC

Parallel GC

-XX:+UseParallelGC 

Tuning Parallel GC

CMS GC (Concurrent Mark & Sweep)

-XX:+UseConcMarkSweepGC

Tuning CMS GC

G1 GC (Garbage first)

-XX:+UseG1GC

Tuning G1 GC

Shenandoah GC

-XX:+UseShenandoahGC

Tuning Shenandoah GC

ZGC

-XX:+UseZGC

Tuning ZGC GC

Epsilon GC

-XX:+UseEpsilonGC

N/A


Evaluating GC Performance

Before you commit to an algorithm for your application, it’s best to evaluate its performance. To do so, you’ll need to request a GC log, and then analyze it.

1. Requesting a GC Log

Use JVM command line switches.

For Java 9 or later:

Java
 
-Xlog:gc*:file=<gc-log-file-path>


For Java 8 or earlier:

Java
 
-XX:+PrintGCDetails -Xloggc:<gc-log-file-path>


2. Evaluating the Log

You can open the log with any text editor, but for long-running programs, it could take hours to evaluate it.

The log information looks something like this:

Java
 
[0.082s][info][gc,heap] Heap region size: 1M
[0.110s][info][gc     ] Using G1
[0.110s][info][gc,heap,coops] Heap address: 0x00000000c8c00000, size: 884 MB, Compressed Oops mode: 32-bit
[0.204s][info][gc,heap,exit ] Heap
[0.204s][info][gc,heap,exit ]  garbage-first heap   total 57344K, used 1024K [0x00000000c8c00000, 0x0000000100000000)
[0.204s][info][gc,heap,exit ]   region size 1024K, 2 young (2048K), 0 survivors (0K)
[0.204s][info][gc,heap,exit ]  Metaspace       used 3575K, capacity 4486K, committed 4864K, reserved 1056768K
[0.204s][info][gc,heap,exit ]   class space    used 319K, capacity 386K, committed 512K, reserved 1048576K


A good choice for quickly obtaining meaningful stats from the GC log is the GCeasy tool. It detects memory leaks, highlights long GC pauses and inefficient GC cycles, as well as making performance tuning recommendations.

Below is a sample of part of the GCeasy report.

Fig: Sample of GCeasy Output

Conclusion

In this article, we’ve looked at the different types of Java Garbage collectors, and learned how to invoke each algorithm on the JVM command line. We’ve looked briefly at how to evaluate, monitor, and tune the garbage collector.

G1 GC is a good all-around choice if you're using Java 8 and above. It was still experimental in Java 7, so it may not be stable. If you have a very large heap size, consider Shenandoah or Z. Again, these may not be stable in earlier versions of Java.

CMS was found to be problematic, as in some cases it caused long GC pauses (as much as 5 minutes), and was therefore deprecated and then finally removed from newer versions.

For more information, you may like to read these articles:

  • Comparing Java GC Algorithms: Which One is Best?
  • What is Java’s default GC algorithm?
Garbage (computer science) garbage collection Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java Z Garbage Collector (ZGC): Revolutionizing Memory Management
  • How Java Apps Litter Beyond the Heap
  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!