A logo showing the text blog.marcnuri.com
Español
Home»Java»JVM Garbage Collectors for the Cloud

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

JVM Garbage Collectors for the Cloud

2024-06-30 in Java tagged Java / JVM / Memory / Performance / Cloud / Kubernetes by Marc Nuri | Last updated: 2025-01-19
Versión en Español

Introduction

Garbage Collection is a fundamental part of the Java Virtual Machine (JVM) that frees up memory by removing objects that are no longer needed by the application. The JVM has several garbage collectors, each with its own characteristics and performance trade-offs.

In the cloud, garbage collection is even more critical, as applications often have to deal with large amounts of data, high traffic, and run in resource-constrained environments. In addition, cloud-native applications are expected to be scalable and sometimes ephemeral. If garbage collection is not managed correctly, it can lead to performance degradation, increased latency, and even application downtime.

In this post, we will explore the different garbage collectors available in the JVM and how to choose the right one for your cloud-native applications.

What is Garbage Collection?

Garbage Collection is the process of automatically freeing up memory by removing objects that are no longer needed or in use by the application. It saves developers from having to manage memory manually, making it easier to write and maintain Java applications.

The JVM has a garbage collector that runs in the background, periodically scanning the heap for objects that are no longer needed. When it finds an object that is no longer referenced by the application, it marks it for deletion and reclaims the memory it was using.

Types of Garbage Collectors

The JVM has several garbage collectors, each with its own characteristics and performance trade-offs. The most common garbage collectors are:

Serial Garbage Collector

This is the simplest type of garbage collection algorithm. It stops all threads in the application and then scans through the heap to find any objects that are no longer being used.

Parallel Garbage Collector

This garbage collector is similar to the serial garbage collector but uses multiple threads to scan through the heap. This can improve performance by taking advantage of multiple CPU cores. It's more suitable for applications that require high throughput and can tolerate short pauses.

Concurrent Mark and Sweep (CMS) Garbage Collector

This algorithm is a more sophisticated type of parallel garbage collection. It uses two threads: a marker thread and a sweeper thread. The marker thread marks all objects that are still being used, and the sweeper thread then frees up any objects that are not marked. It's often used in applications that require low latency and can't tolerate long pauses.

G1 Garbage Collector

The Garbage First (G1) garbage collector is a newer, low-latency garbage collector that is designed to work well with large heaps. It splits the heap into regions and prioritizes collecting garbage from regions with the most garbage. It balances throughput and latency by dynamically adjusting the size of the regions based on the amount of garbage in each region.

Shenandoah Garbage Collector

This is another newer garbage collection algorithm that is designed for large heaps and ultra-low latency applications. It uses a technique called evacuation to move objects from one region of the heap to another. This can help to reduce the amount of fragmentation in the heap.

This garbage collector could be well suited for real-time applications that require low latency and can't tolerate long pauses.

Z Garbage Collector

The Z Garbage Collector is a scalable garbage collector that is designed to work well with large heaps and low-latency applications. It uses a technique called region counting to track the number of objects in each region of the heap. This can help to improve the performance of garbage collection.

Choosing the Right Garbage Collector

The best garbage collection algorithm for your application will depend on a number of factors, including the size of your heap, the amount of memory that your application uses, and the performance requirements of your application.

In addition, you should take into account the characteristics of the garbage collector, such as:

  • Parallel: Whether the garbage collector uses multiple threads to scan through the heap.
  • Concurrent: Whether the garbage collector can run concurrently with the application.
  • Throughput: The amount of work the garbage collector can do in a given amount of time.
  • Latency: The time it takes for the garbage collector to complete a collection cycle.
  • Footprint: The amount of memory the garbage collector uses.

The following table summarizes the characteristics of the different garbage collectors:

Garbage CollectorParallelConcurrentThroughputLatencyFootprint
SerialNoNoLowHighSmall-Medium
ParallelYesNoHighMediumMedium-Large
CMSYesPartiallyMediumMediumMedium-Large
G1YesPartiallyHighLowMedium-Large
ShenandoahYesYesVery HighVery LowLarge
ZYesYesVery HighVery LowLarge

When choosing a garbage collection algorithm, it is important to consider the trade-offs between different algorithms. Some algorithms, such as serial garbage collection, have low pause times but can have low throughput. Other algorithms, such as G1 garbage collection, have high throughput but can have longer pause times.

Since your applications are running in the cloud, you should also consider the resource constraints of your environment. The number of CPU cores, the amount of memory, and the network bandwidth can all affect the performance of the garbage collector.

Ultimately, the best way to choose a garbage collection algorithm is to experiment with different algorithms and see which one works best for your application.

Conclusion

Garbage collection is an important part of any cloud-native application. There are a number of different garbage collection algorithms that can be used, each with its own advantages and disadvantages.

In this post, we have explored the different garbage collectors available in the JVM and how to choose the right one for your cloud-native applications. By understanding the characteristics of each garbage collector and experimenting with different algorithms, you can optimize the performance of your applications and ensure that they run smoothly in the cloud.

References

This article was heavily inspired by the talk GC Algorithms for the Cloud by Pratik Patel at the DevBcn 2024. Make sure to check out the talk for a more in-depth look at the topic.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Eclipse JKube 1.17 is now available!DevBcn 2024 - Barcelona
© 2007 - 2025 Marc Nuri