All 7 Java Garbage Collectors Explained

 

Transcript:

Today we are talking about garbage collection in Java. Hotspot JVM offers seven garbage collectors for various performance requirements. Let's look at all of them.

But first, a couple of words about how Java memory model works. Garbage collection in Java is automatic. An object is considered eligible for GC when it becomes unreachable, meaning that there are no references to it. Such objects are deleted by the garbage collector. One of the functions of a Java garbage collection is managing objects in generations. Mul created objects belong to the young generation. As a rule, most objects die young, but if they don't, they are promoted to the old generation or tenure generation. These generations occupy different spaces on the JVM hip. Eden space and two survivor spaces zero and one are where young objects live. Tenure space preserves the old generation. Metaspace stores metadata. It replaced perm used in Java versions earlier than JDK8. When any of these spaces fill up, garbage collection occurs. A minor collection happens when the young space stops up. If some objects are still used at the moment of garbage collection, they are promoted to uh survivor space zero and then to survivor space one. If they're still alive in survivor space one, they are promoted to uh old generation. As a result, they are moved to the tenure space. At some point, tenure space also gets filled up and that's when the major collection occurs. Major collections usually take more time because more objects are involved.

And now let's look at various Java garbage collectors in more detail. Serial GC is the simplest and the oldest collector in Java. It works in one thread and freezes all application threads while performing the collection. It is good for single threaded client side applications that uh don't uh require extra small post times. But there is a catch. If you limit your application to 2 GB of RAM and less than two processors, then the serial GC is enabled by default even if you explicitly enable another collector. So be careful about that.

Parallel GC is also called the throughput collector. It also freezes all application threads but works in multiple threads to perform the collection. By default, it works in multiple threads when uh performing the collection of the young generation and the old generation. You can set the number of worker threads, post times, and the time the application spends on a collection.

Concurrent mark sweep GC was used in earlier Java versions. It was designed for shorter poses and used multiple threads to perform the collection, but it is suitable for applications that can afford to share processor resources with the garbage collector. This collector was duplicated in Java 9 and removed in Java 14. G1GC was designed to replace CMSGC. It is now default garbage collector in Java. G1GC tries to provide a good balance between latency and throughput. It is a good fit for applications uh running in multiprocessor environments with heaps of 10 GB and more. G1 means garbage first. Just like other collectors, it works with young and old generations.

However, it splits the heap into multiple equals sized regions. First, it performs the global marking phase to determine which objects are alive. It does it concurrently with the application. Then it reclaims the space in the regions that are mostly filled with garbage. This way, it can free more space. This is why it's called garbage first. It performs the collection using evacuation. It copies and compacts objects from one or several regions into one or several regions. This way it avoids memory fragmentation. You can also tune this collector. You can set pause times, pause intervals and so on. ZC was first introduced in Java 11. It is a scalable low latency collector which is perfect for latency sensitive applications and applications with a very large heap of terabytes. ZC performs the expensive work concurrently and doesn't stop the application threads for more than 10 milliseconds. It requires minimal configuration.

In most cases, you just uh enable this collector and set uh the heap size and you're good to go. In Java 23, it became generational by default. It means that it maintains uh separate generations for young and old objects and collects young objects more frequently. It improves heap memory overhead and the garbage collection CPU overhead. Shannonova GC is also a low latency collector. It was first introduced in Java 12. It performs most of its work concurrently with the application including concurrent compaction. Therefore, the pulse times are not directly proportional to the hip size. Like Zachy, it is suitable for latency sensitive applications and applications with large heaps of terabytes. It became generational by default in JDK25. Oracle Java doesn't ship Shannono GC but it is available uh with various open GDK distributions including libera GDK.

Epsilon GC is a very interesting collector. It doesn't perform garbage collection and only allocates memory. Why would you need a garbage collector that doesn't collect any garbage you may ask? Well actually it has a purpose. First, you can use it for performance testing to see how fast the application will run without any garbage collection and see whether there are any performance bottlenecks that are not related to garbage collection. Also, you can use it with applications that almost don't allocate any garbage or you can use it with shortlived applications that don't have time to produce any garbage. This way, they may run faster without the garbage collection cycles. Another interesting feature of epsilon GC is that it will remain experimental forever. This is done to avoid enabling it accidentally in the production. So to use epsilon GC you need to explicitly unlock experimental VM options. And that was it about available garbage collectors in hotspot JVM.

Summary

This video explains how Java garbage collection works and compares all seven garbage collectors available in the HotSpot JVM. It describes the Java memory model, including young and old generations, as well as how minor and major collections occur. The video then reviews each collector: Serial GC for simple single-threaded apps, Parallel GC for higher throughput, the deprecated CMS, and G1GC—the current default designed for balanced performance. It also covers low-latency collectors like ZGC and Shenandoah, which perform most work concurrently and support huge heaps, and Epsilon GC, which performs no garbage collection and is useful for performance testing or short-lived applications. The guide highlights when each collector is appropriate and how they differ in latency, throughput, and scalability.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Feb 6, 2026
Backend Developer Roadmap 2026: What You Need to Know

Backend complexity keeps growing, and frameworks can't keep up. In 2026, knowing React or Django isn't enough. You need fundamentals that hold up when systems break, traffic spikes, or your architecture gets rewritten for the third time.I've been building production systems for 15 years. This roadmap covers three areas that separate people who know frameworks from people who can actually architect backend systems: data, architecture, and infrastructure. This is about how to think, not what tools to install.

Videos
card image
Jan 29, 2026
JDBC Connection Pools in Microservices. Why They Break Down (and What to Do Instead)

In this livestream, Catherine is joined by Rogerio Robetti, the founder of Open J Proxy, to discuss why traditional JDBC connection pools break down when teams migrate to microservices, and what is a more efficient and reliable approach to organizing database access with microservice architecture.

Further watching

Videos
card image
Feb 27, 2026
Spring Developer Roadmap 2026: What You Need to Know

Spring Boot is powerful. But knowing the framework isn’t the same as understanding backend engineering. In this video, I walk through the roadmap I believe matters for a Spring developer in 2026. We start with data. That means real SQL — CTEs, window functions, normalization trade-offs — and understanding what ACID and BASE actually imply for system guarantees. Spring Data JPA is useful, but you still need to know what happens underneath. Then architecture: microservices vs modular monolith, serverless, CQRS, and when HTTP, gRPC, Kafka, or WebSockets make sense. Not as buzzwords — but as design choices with trade-offs. Security and infrastructure follow: OWASP Top 10, AuthN vs AuthZ, encryption in transit and at rest, Docker, Kubernetes, Infrastructure as Code, and observability with Micrometer, OpenTelemetry, and Grafana. This roadmap isn’t about mastering every tool. It’s about knowing what affects reliability in production.

Videos
card image
Feb 18, 2026
Build Typed AI Agents in Java with Embabel

Most Java AI demos stop at prompt loops. That doesn't scale in production. In this video, we integrate Embabel into an existing Spring Boot application and build a multi-step, goal-driven agent for incident triage. Instead of manually orchestrating prompt → tool → prompt cycles, we define typed actions and let the agent plan across deterministic and LLM-powered steps. We parse structured input with Ollama, query MongoDB deterministically, classify risk using explicit thresholds, rank affected implants, generate a constrained root cause hypothesis, and produce a bounded containment plan. LLM handles reasoning. Java enforces rules. This is about controlled AI workflows on the JVM — not prompt glue code.

Videos
card image
Feb 12, 2026
Spring Data MongoDB: From Repositories to Aggregations

Spring Data MongoDB breaks down fast once CRUD meets production—real queries, actual data volumes, analytics. What looks simple at first quickly turns into unreadable repository methods, overfetching, and slow queries. In this video, I walk through building a production-style Spring Boot application using Spring Data MongoDB — starting with basic setup and repositories, then moving into indexing, projections, custom queries, and aggregation pipelines. You'll see how MongoDB's document model changes data design compared to SQL, when embedding helps, and when it becomes a liability. We cover where repository method naming stops scaling, how to use @Query safely, when to switch to MongoTemplate, and how to reduce payload size with projections and DTOs. Finally, we implement real MongoDB aggregations to calculate analytics directly in the database and test everything against a real MongoDB instance using Testcontainers. This is not another MongoDB overview. It's a practical guide to actually using Spring Data MongoDB in production without fighting the database.