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 – 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 – 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

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.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

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

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

Java Virtual Machine (JVM) serviceability tools, such as Dynamic Attach and the Serviceability Agent, are invaluable for managing Java applications in complex production environments. While both provide insight into the JVM behavior, they work differently and are suited for distinct scenarios.

In this tutorial, we’ll investigate key differences and similarities between them. We’ll get a thorough understanding of how each one works, along with its advantages, and limitations.

We’ll likewise look at a use case showing how to benefit from each. By the end of the article, we’ll be well-positioned to choose the most suitable solution for our needs. We’ll focus on one of the Latest Long-Term Support (LTS) releases of the JVM, JDK 21, which features the Generational ZGC.

2. Dynamic Attach: Definition and Features

Dynamic Attach is a feature of the JVM that allows tools and agents to attach to a running Java process at runtime. This capability doesn’t require the application to restart or pre-configure.

This mechanism uses the Attach API to connect to the target JVM, enabling the dynamic loading of Java agents and the execution of diagnostic commands. Dynamic Attach provides some features that we’ll consider in the following sub-sections.

2.1. Real-Time Diagnostics and Monitoring

Through Dynamic Attach, we can establish a live connection to a JVM process and monitor its health in real-time. Using resources such as jcmd, JConsole, or VisualVM, we can delve into various metrics pertaining to the JVM. These include memory consumption, garbage collection behavior patterns, thread states, and CPU utilization.

This feature can diagnose performance issues or detect bottlenecks without interrupting operations. Additionally, it allows the identification of problematic areas such as threads consuming high CPU resources or frequent GC pauses.

2.2. Loading Agents Dynamically

Dynamic Attach’s strength lies in its capability to load Java agents dynamically into a running JVM process. These agents can instrument bytecode at runtime, thus allowing them to monitor or modify an application’s behavior.

Dynamic loading of agents is allowed in JDK 21, although with a warning from the JVM. Starting with JDK 21, future releases will disable it by default. Developers must use the command XX:+EnableDynamicAgentLoading to enable this feature when launching their applications.

2.3. Minimal Performance Overhead

The Attach API and tools like jstack use Dynamic Attach to interact with running applications.

They enable actions like collecting thread dumps, retrieving garbage collection logs, and attaching monitoring agents. These tools are designed to minimize performance overhead.

3. Some Limitations of Dynamic Attach

Complex applications with many interactions may slow down diagnostic tools or consume significant resources.

Furthermore, compatibility issues might arise due to limited support for Attach API in some JVM variants.

Although collecting diagnostics such as thread dumps and garbage collection logs can be advantageous, their excessive usage could compromise application performance. Consequently, careful monitoring is needed to prevent the system from being overwhelmed by excessive data.

Lastly, strict access controls in some environments might restrict permission to attach to certain processes.

4. Serviceability Agent

The Java Virtual Machine offers the Serviceability Agent, which is a diagnostic tool for the thorough exploration and evaluation of internal data structures.

4.1. Access to Internal JVM Structures

The Serviceability Agent provides a powerful interface for examining JVM internals. These include object memory layouts, symbol tables, class loaders, thread states, garbage collector information, and more.

Such a feature proves useful when dealing with complex issues that can’t be identified through high-level JVM metrics alone.

4.2. Post-Mortem Debugging

When a JVM experiences a crash, it typically produces a comprehensive snapshot of its state, known as a core dump.

The Serviceability Agent can attach to this core dump and examine its contents to determine the underlying cause of the crash. Thus, SA is valuable for performing post-mortem analysis of such events.

4.3. Heap Dump and Object Analysis

Through the SA, users can perform in-depth analysis of heap dumps to gain insight into the JVM’s heap and the objects it contains.

This tool enables the inspection of memory usage patterns, objects, and references.

4.4. Thread and Lock Analysis

The Serviceability Agent provides detailed insights into thread states, locks, and synchronization issues. It can identify threads that are deadlocked, blocked, or waiting, helping diagnose performance problems caused by thread contention or locking issues.

Administrators can use the SA to detect bottlenecks and optimize thread management, ensuring smoother application performance.

5. Some Limitations of the Serviceability Agent

The agent could cause a decrease in performance, especially when collecting extensive diagnostic details. This can impact the application’s responsiveness under high-load scenarios.

In addition, in environments with strict access controls, the agents may face restrictions. Security limitations could hinder their ability to attach to specific processes. This constraint may limit its usefulness in production environments.

6. Practical Use Case: Diagnosing and Resolving a Memory Leak

Let’s consider a scenario where a memory leak is causing gradual performance degradation in production for a large-scale Java application. We’ll see how we can apply Dynamic Attach and the Serviceability Agent.

6.1. Initial Investigation With Dynamic Attach

We adopt a three-part strategy, using Dynamic Attach for initial diagnosis.

The first step involves generating a heap dump through the jcmd command. This is followed by memory and garbage collection monitoring via JConsole.

Finally, profiling agent deployment is performed through VisualVM. These steps yield comprehensive and non-disruptive insights into application performance metrics and memory allocation data. This information can be used for further analysis if necessary.

6.2. Detailed Analysis With the Serviceability Agent

During the detailed analysis phase, we use the Serviceability Agent to delve deeper into memory issues. The process involves three steps: Dump analysis, crash investigation, and custom tool development.

Firstly, we analyze previously generated heap dumps to identify potential leak sources and object retention patterns.

Secondly, if an out-of-memory crash occurs, core dumps are analyzed to gain insights into the JVM’s final state.

Lastly, we develop a custom Serviceability Agent tool. This tool traverses through the heap and identifies specific object types or patterns contributing to memory leaks.

This methodology provides detailed insights into complex issues inside JVMs.

6.3. Resolution and Verification

After identifying and resolving the source of a memory leak, we implement a structured strategy to prevent future occurrences. This encompassed agent deployment, custom metrics exposure using JMX pertaining to previously leaking resources, and continuous monitoring.

Through this strategy, we can watch over potential recurrences or new memory-related issues.

7. Summary

To  illustrate the differences between Dynamic Attach and the Serviceability Agent, let’s examine a comparison table:

Feature Dynamic Attach Serviceability Agent
Real-time Monitoring Yes No
Dynamic Agent Loading Yes No
Performance Overhead Designed to minimize performance impact Performance degradation when collecting extensive data.
Scope of Operations Focused on live applications and ongoing monitoring Includes live and post-mortem analysis
Use Cases Monitoring, profiling, immediate diagnostics Deep analysis, troubleshooting, post-mortem debugging
Data Accessibility Limited to real-time data collection and analysis Comprehensive JVM internals

This table shows how Dynamic Attach and the Serviceability Agent complement each other. The former is ideal for real-time monitoring while the latter excels in JVM analysis, especially in post-mortem scenarios.

8. Conclusion

In this article, we delved into the functions of Dynamic Attach and the Serviceability Agent in monitoring and resolving issues in the JVM.

The Serviceability Agent offers in-depth inspection of JVM internals for thorough analysis and post-mortem debugging. On the other hand, Dynamic Attach performs well in instantaneous diagnostics and low-impact monitoring.

Despite their limitations, both tools can enhance the performance and reliability of the JVM.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)