A glance on Java MBeans

Ranjith Raj D
2 min readJan 28, 2021

--

MXBeans

The Java Management Extensions (JMX) API is a standard API for management and monitoring of resources. This JMX Api is namely Managed Beans or MBeans.
MBean is a managed Java object, like the Java Beans component that follows JMX specification. This MBean is to manage any applications,system objects, devices, services, and the Java virtual machine. Managing and monitoring applications can be designed and developed using the Java Dynamic Management Kit (Java DMK). This short writeup is about java management extension to monitor your application with simpler code for experimental purpose.

MXBeans is one of the types of MBeans. The main idea behind MXBeans is for Memory usage. There are two types of MXBeans for JVM garbage collector.
- Young
- Old

This small code snippet is going to give high-level usage of GarbageCollectorMXBean.

```java
package com.ran;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

public class MXTest {
public static void main(String[] args) {
List<GarbageCollectorMXBean> mxBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean garbageCollectorMXBean : mxBeans) {
System.out.println(“Name : “+garbageCollectorMXBean.getName());
System.out.println(“Collection time : “+mxBean.getCollectionTime() + “ ms”);
System.out.println(“Number of collections : “+mxBean.getCollectionCount());

for (String pool : garbageCollectorMXBean.getMemoryPoolNames()) {
System.out.println(“\t”+pool);
}

System.out.println();

}
}
}

```

If you run this, you will not see any information about the collection, as no GC performed yet. This is just to give an idea about how these beans are working and how to use them in your application for monitoring and audit purpose.

This is the output of the above snippet using OpenJdk version “1.8.0_222”. It is the result of default garbage collectors MBean. It may vary based on the JVM implementation and garbage collector.

```
Name : PS Scavenge
Collection time : 0 ms
Number of collections : 0
PS Eden Space
PS Survivor Space

Name : PS MarkSweep
Collection time : 0 ms
Number of collections : 0
PS Eden Space
PS Survivor Space
PS Old Gen
```

PS Scavenge
This Bean has information about young generation collector.

PS MarkSweep
This bean holds information about the Old generation collector

Check your java’s default GC by executing this command
```
java -XX:+PrintCommandLineFlags -version
```

Using specific GC in the JVM argument may provide a different result. Below result is produced after choosing CMS GC (Concurrent Marke Sweep GC)
``` -XX:+UseConcMarkSweepGC ```

```
Name : ParNew
Collection time : 0 ms
Number of collections : 0
Par Eden Space
Par Survivor Space

Name : ConcurrentMarkSweep
Collection time : 0 ms
Number of collections : 0
Par Eden Space
Par Survivor Space
CMS Old Gen

```

You can try to pass a different parameter for garbage collectors and see how those parameters affect the garbage collectors that get used for your Application.

JMX Spec : https://docs.oracle.com/javase/7/docs/technotes/guides/jmx/spec.html

--

--

Ranjith Raj D
Ranjith Raj D

Written by Ranjith Raj D

Software Architect ✦ Full stack developer ✦ Artist ✦ Autodidact ✦ Codeaholic ✦ https://www.linkedin.com/in/ranjithrajd/

No responses yet