JMX: A Cool Tool for Java Developers

Last Updated: • 4 min read
Table of Contents

Have you heard about JMX, also known as “Java Management Extensions”? I hadn’t, and when I discovered it, I was amazed and delighted. Despite studying Java in school and taking Java OOP courses during my BSc degree, JMX was never mentioned. Let me share my experience with this powerful tool.

Context

I recently joined a new team at work and was tasked with improving one of our internal Java services. The project’s size and complexity quickly overwhelmed me. I started with some familiar approaches to find bottlenecks:

  1. Removing parts of the code and timing to see if any significant changes occurred
  2. Checking for known bugs in library versions
  3. Refactoring some functions

These methods proved tedious and inefficient for identifying issues in the code. That’s when I stumbled upon JMX.

What is JMX?

JMX, or Java Management Extensions, is a powerful tool that comes bundled with the Java Development Kit (JDK). To understand its significance, let’s briefly review how Java works:

  1. Java code is compiled into Java bytecode
  2. The bytecode runs on a Java Virtual Machine (JVM)
  3. The JVM executes the code on the machine

The JVM handles various tasks, including garbage collection and memory allocation. JMX is one of its features, allowing you to monitor and manage Java applications at runtime.

To enable JMX, you need to pass specific arguments when running your Java application:

-Djava.rmi.server.hostname=127.0.0.1
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.rmi.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

These arguments tell the JVM to enable JMX connections and listen on port 9010. For more information, check the official documentation or this helpful StackOverflow post.

What JMX Offers

JMX provides a wealth of information and tools for monitoring and managing Java applications. To leverage its capabilities, I used two open-source software tools:

  1. VisualVM - A lightweight tool with essential features
  2. JDK Mission Control - A feature-rich JMX (and more) visualizer

I recommend starting with VisualVM and moving to JDK Mission Control if you need more advanced features.

Important Note: JMX allows you to connect to Java instances both locally and remotely. This capability can be both beneficial and potentially risky. Theoretically, you could inspect Java services running behind firewalls during runtime and gather valuable information. Many exporters and products leverage this feature.

Let’s explore some key features of VisualVM:

1. Application Overview

visualvm-homepage.png The overview tab provides essential information about the JVM:

This information is invaluable for debugging. For example, in my case, I discovered we were using Java version 17, which could be upgraded for better performance.

2. Thread States

The threads tab displays the state of each thread in the system:

threads-page.png

Java threads can be in the following states:

Java Thread VisualVM Naming Explanation
NEW Zombie A thread that has not yet started
RUNNABLE Running A thread executing in the JVM
BLOCKED Monitor A thread waiting for a monitor lock
WAITING Sleeping/Park/Wait A thread waiting indefinitely for another thread to perform an action
TIMED_WAITING Sleeping/Park/Wait A thread waiting for another thread to perform an action for up to a specified time
TERMINATED Zombie A thread that has exited

This view helps identify issues like excessive context switching or bottlenecks caused by I/O operations. For a deeper understanding of context switching and its impact on performance, I recommend reading Operating System Concepts.

3. Monitor View

The Monitor view displays CPU and RAM usage by the JVM in easy-to-read diagrams. You can also observe garbage collector activity here.

monitors-page.png

4. Sampler View

This is where JMX truly shines. The Sampler view helps you identify which functions consume the most CPU time or memory.

sampler-page.png

The CPU Sampler shows which functions use the most processor time:

sampler-cpu-page.png

The Memory Sampler displays which functions or classes use the most memory, helping you identify potential memory leaks:

sampler-mem-page.png

Wrapping Up

JMX is a powerful tool that can significantly speed up the process of finding and resolving performance issues in Java applications. When I discovered it, it helped me quickly identify root problems in our large service that would have taken much longer to find otherwise.

Additional Features

VisualVM supports plugins, including a beans viewer that shows useful metrics for various components like Kafka clients used by Spring. I highly recommend exploring the plugins page and downloading those relevant to your project.

plugins-view.png

While JDK Mission Control offers even more features and options, it can be overwhelming at first. I suggest starting with VisualVM and moving to more advanced tools as needed.

By leveraging JMX and tools like VisualVM, you can gain deep insights into your Java applications’ performance and behavior, making debugging and optimization much more efficient.

Tags: