How to monitor memory with Java 10’s improved GC interface

Java 10 introduced enhancements to the Garbage Collection (GC) interface through the JEP 304: GC Interface, which abstracts garbage-collection implementations to improve integration and monitoring capabilities. While these improvements primarily simplify the addition of new garbage collectors to the JVM, they can also be leveraged to monitor memory usage and GC behavior in real time.

Here’s how to monitor memory using Java 10’s improved GC interface.

Key Concepts

The primary tools for monitoring memory and garbage collection (from Java 10 onward) include:
1. java.lang.management package: Interfaces and classes such as GarbageCollectorMXBean, MemoryMXBean, and MemoryPoolMXBean are still accessible.
2. java.util.logging or external libraries: For logging GC activity.
3. New Unified Logging framework: Can be used to log GC activities in detail starting with Java 9.


Steps to Monitor Memory Using Java 10 GC Interface:

1. Use the GarbageCollectorMXBean

The GarbageCollectorMXBean allows you to track details such as the number of collections, total collection time, and more.

Here’s an example:

package org.kodejava.lang.management;

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

public class GcMonitoringDemo {
    public static void main(String[] args) {
        // Get all GC beans
        List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();

        for (GarbageCollectorMXBean gcBean : gcBeans) {
            System.out.println("Garbage Collector: " + gcBean.getName());
            System.out.println("Collection count: " + gcBean.getCollectionCount());
            System.out.println("Collection time (ms): " + gcBean.getCollectionTime());
        }

        // Simulate some memory load
        for (int i = 0; i < 10000; i++) {
            String[] temp = new String[1000];
            temp = null; // Let the memory be collected
        }

        System.out.println("After memory load:");
        for (GarbageCollectorMXBean gcBean : gcBeans) {
            System.out.println("Garbage Collector: " + gcBean.getName());
            System.out.println("Collection count: " + gcBean.getCollectionCount());
            System.out.println("Collection time (ms): " + gcBean.getCollectionTime());
        }
    }
}

Output will include:

  • Garbage collector names based on the JVM (e.g., G1 Young Generation, G1 Old Generation, etc.).
  • Collection count and total collection time.

2. Analyze Memory Usage via the MemoryMXBean

The MemoryMXBean interface helps monitor heap and non-heap memory usage.

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

public class MemoryMonitoringDemo {
    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

        // Get heap memory usage
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        System.out.println("Heap Memory Usage:");
        System.out.println("  Init: " + heapMemoryUsage.getInit());
        System.out.println("  Used: " + heapMemoryUsage.getUsed());
        System.out.println("  Max: " + heapMemoryUsage.getMax());
        System.out.println("  Committed: " + heapMemoryUsage.getCommitted());

        // Get non-heap memory usage
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        System.out.println("Non-Heap Memory Usage:");
        System.out.println("  Init: " + nonHeapMemoryUsage.getInit());
        System.out.println("  Used: " + nonHeapMemoryUsage.getUsed());
        System.out.println("  Max: " + nonHeapMemoryUsage.getMax());
        System.out.println("  Committed: " + nonHeapMemoryUsage.getCommitted());
    }
}

3. Monitor GC Using Unified Logging

Starting from Java 9, the new Unified Logging Framework allows you to log GC activities comprehensively. You can enable it with various JVM options.

For example:

java -Xlog:gc* -XX:+UseG1GC -jar YourApplication.jar

Additional useful options include:

  • -Xlog:gc+heap: Logs GC and heap events.
  • -Xlog:gc+age: Logs information about object aging.
  • -Xlog:gc*=info,safepoint: Logs GC and safe-point information.

Output in the log will provide in-depth GC activity for analysis.


4. Advanced Real-Time Monitoring with JFR (Java Flight Recorder)

Java Flight Recorder (JFR) is another tool integrated into the JVM that enables detailed profiling and monitoring, including GC data.

java -XX:StartFlightRecording=filename=recording.jfr,duration=60s -XX:+UnlockCommercialFeatures -jar YourApplication.jar

After this recording, you can analyze recording.jfr in tools such as Java Mission Control (JMC).


5. Third-Party Tools for Active Monitoring

You can also leverage external tools or libraries:

  • VisualVM: Provides a GUI-based approach to monitor GC and memory usage.
  • micrometer.io: A metrics library for monitoring in microservices.
  • Prometheus + Grafana: To build custom dashboards for GC and memory metrics.

Conclusion

  • For basic JVM-based monitoring, use the GarbageCollectorMXBean and MemoryMXBean.
  • For detailed runtime logging of GC behavior, use the Unified Logging Framework.
  • For comprehensive profiling and diagnostics, use tools like JFR or VisualVM.

Java 10’s GC interface improvements make it easier to add and monitor new garbage collector implementations, but the existing Java Management Extensions (JMX) and logging tools are still central to effective memory monitoring.

How do I get peak live threads count?

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class PeakThreadCount {
    public static void main(String[] args) {
        // Get the managed bean for the thread system of the Java
        // virtual machine.
        ThreadMXBean bean = ManagementFactory.getThreadMXBean();

        // Get the peak live thread count since the Java virtual
        // machine started or peak was reset.
        int peakThreadCount = bean.getPeakThreadCount();
        System.out.println("Peak Thread Count = " + peakThreadCount);
    }
}

How do I get current number of live threads?

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class ThreadCount {

    public static void main(String[] args) {
        // Get the managed bean for the thread system of the Java
        // virtual machine.
        ThreadMXBean bean = ManagementFactory.getThreadMXBean();

        // Get the current number of live threads including both
        // daemon and non-daemon threads.
        int threadCount = bean.getThreadCount();
        System.out.println("Thread Count = " + threadCount);
    }
}

How do I get the Java classpath?

The RuntimeMXBean.getClassPath() returns the Java class path that is used by the system class loader to search for class files. This method is equivalent to System.getProperty("java.class.path").

Multiple paths in the Java class path are separated by the path separator character of the platform of the Java virtual machine being monitored.

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetClassPath {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String classPath = bean.getClassPath();
        System.out.println("ClassPath = " + classPath);
    }
}

How do I get system properties information?

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Set;

public class GetSystemProperties {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        // Returns a map of names and values of all system
        // properties. This method calls System.getProperties()
        // to get all system properties. Properties whose
        // name or value is not a String are omitted.
        Map<String, String> systemProperties = bean.getSystemProperties();
        Set<String> keys = systemProperties.keySet();
        for (String key : keys) {
            String value = systemProperties.get(key);
            System.out.printf("Property[%s] = %s%n", key, value);
        }
    }
}

Some properties produced by the code snippet above are:

Property[java.specification.version] = 17
Property[sun.cpu.isalist] = amd64
Property[sun.jnu.encoding] = Cp1252
Property[java.vm.vendor] = Oracle Corporation
Property[sun.arch.data.model] = 64
Property[user.variant] = 
Property[java.vendor.url] = https://java.oracle.com/
...
...
Property[java.vm.info] = mixed mode, sharing.
Property[java.vendor] = Oracle Corporation.
Property[java.vm.version] = 17+35-LTS-2724.
Property[sun.io.unicode.encoding] = UnicodeLittle.
Property[java.class.version] = 61.0.