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
andMemoryMXBean
. - 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.