Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until the end of this weekend:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Regression testing is an important step in the release process, to ensure that new code doesn't break the existing functionality. As the codebase evolves, we want to run these tests frequently to help catch any issues early on.

The best way to ensure these tests run frequently on an automated basis is, of course, to include them in the CI/CD pipeline. This way, the regression tests will execute automatically whenever we commit code to the repository.

In this tutorial, we'll see how to create regression tests using Selenium, and then include them in our pipeline using GitHub Actions:, to be run on the LambdaTest cloud grid:

>> How to Run Selenium Regression Tests With GitHub Actions

Course – Black Friday 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until the end of this weekend:

>> EXPLORE ACCESS NOW

1. Introduction

The Garbage Collector (GC) handles the memory management in Java. As a result, programmers don’t need to explicitly take care of memory allocation and deallocation.

In Java, JVM reserves a certain amount of memory at the beginning. Sometimes, the actual memory used is significantly less than the reserved amount. In such scenarios, we prefer to return the excess memory to OS.

This entire process is dependent on the algorithms used for garbage collection. Consequently, we can choose the type of GC and JVM as per the required behavior.

In this tutorial, we’ll explore memory management by GC and its interaction with OS.

2. JVM Memory Organization

When JVM is initialized, different types of memory areas are created inside it, such as Heap area, Stack area, Method Area, PC Registers, and Native Method Stack.

GC deals with heap storage. Hence, we’ll focus on memory interaction related to the heap in this article.

We can specify the initial and maximum heap sizes using the flags -Xms and -Xmx, respectively. If -Xms is lower than -Xmx, it implies that JVM hasn’t committed all the reserved memory to the heap in the beginning. In short, heap size starts from -Xms and can expand up to -Xmx. This allows a developer to configure the size of the required heap memory.

Now, when the application runs, different objects are allocated memory inside the heap. At the time of garbage collection, GC deallocates the unreferenced objects and frees the memory. This deallocated memory is currently a part of the heap itself, as it is a CPU intensive procedure to interact with OS after each deallocation.

Objects reside in a scattered manner inside the heap. GC needs to compact the memory and create a free block to return to OS. It involves an additional process execution while returning the memory. Also, Java applications might need additional memory at a later stage. For this, we need to communicate with the OS again to request more memory. Moreover, we can’t ensure the availability of memory in the OS at the requested time. Hence, it is a safer approach to use the internal heap instead of frequently calling the OS to fetch memory.

However, if our applications don’t require entire heap memory, we’re just blocking the available resources, which could have been used by the OS for other applications. Considering this, the JVM has introduced efficient and automated techniques for memory release.

3. Garbage Collectors

Progressing over different release versions, Java has introduced different types of GCs. Memory interaction between heap and OS is dependent on the JVM and GC implementations. Some GC implementations actively support heap shrinking. Heap shrinking is the process of releasing back the excess memory from heap to OS for optimal resource usage.

For example, Parallel GC doesn’t release unused memory back to the OS readily. On the other hand, some GCs analyze the memory consumption and determine accordingly to release some free memory from the heap. G1, Serial, Shenandoah, and Z GCs support heap shrinking.

Let’s explore these processes now.

3.1. Garbage First (G1) GC

G1 has been the default GC since Java 9. It supports compaction processes without lengthy pauses. Using internal adaptive optimization algorithms, it analyzes the RAM required as per application usage and uncommits the memory if required.

Initial implementations support heap shrinking either after full GC or during concurrent cycle events. However, for an ideal situation, we want to promptly return the unused memory to the OS, especially for the periods when our application is idle. We want the GC to dynamically adapt to the memory usage by our applications at runtime.

Java has included such capabilities in different GCs. For G1, JEP 346 introduces these changes. From Java 12 and higher, heap shrinking is also possible in the concurrent remark phase. G1 tries to analyze the heap usage when the application is idle and triggers the periodic garbage collection as needed. G1 can either start a concurrent cycle or a full GC based on the G1PeriodicGCInvokesConcurrent option. After the cycle executes, G1 needs to resize the heap and return freed memory back to OS.

3.2. Serial GC

Serial GC also supports heap shrinking behavior. In comparison to G1, it requires additional four full GC cycles to uncommit freed memory.

3.3. ZGC

ZGC was introduced with Java 11. It was also enhanced with the functionality to return unused memory to OS in JEP 351.

3.4. Shenandoah GC

Shenandoah is a concurrent GC. It performs the garbage collection asynchronously. Eliminating the need for full GC greatly helps in the performance optimization of the application.

4. Using JVM Flags

We’ve previously seen that we can specify heap sizes using JVM command-line options. Similarly, we can use different flags to configure the default heap shrinking behavior of a GC :

  • -XX:GCTimeRatio: To specify the desired time split between application execution and GC execution. We can use it to make the GC run longer
  • -XX:MinHeapFreeRatio: To specify the minimum expected proportion of free space in the heap after garbage collection
  • -XX:MaxHeapFreeRatio: To specify the maximum expected proportion of free space in the heap after garbage collection

If the available free space in the heap is higher than the ratio specified using -XX:MaxHeapFreeRatio option, then GC can return the unused memory to the OS. We can configure the value of the above flags to constrain the amount of unused memory in the heap. We’ve similar options available for concurrent garbage collection processes:

  • -XX:InitiatingHeapOccupancyPercent: To specify the heap occupancy percentage required to start a concurrent garbage collection.
  • -XX:-ShrinkHeapInSteps: To reduce the heap size to the -XX:MaxHeapFreeRatio value immediately. The default implementation requires multiple garbage collection cycles for this process.

5. Conclusion

In this article, we’ve seen that Java provides different types of GCs catering to different requirements. GC can reclaim and return the free memory to the host OS. We can choose the type of GC as per our requirements.

We’ve also explored the use of JVM parameters to tweak the GC behavior to reach desired performance levels. Additionally, we can opt for dynamic scaling of memory utilization by JVM. We should consider the trade-offs related to each chosen option for our application and the resources involved.

Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until the end of this weekend:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2025 – NPI (All)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until next Monday:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments