#Week 24 — What is the `RuntimeMXBean? in Java?
3 messages · Page 1 of 1 (latest)
RuntimeMXBean is a Java class part of the java.management module (which provides methods for managing JVMs/running Java applications) and allows inspecting information about a JVM. This is possible for the current JVM but also other JVMs.
A RuntimeMXBean for the current JVM can be obtained using ManagementFactory.getRuntimeMXBean():
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
//general JVM information
String vmName = bean.getVmName();
String vendorName = bean.getVmVendor();
String version = bean.getVmVersion();
//information about the spec implemented by the JVM
String specName = bean.getSpecName();
String specVersion = bean.getSpecVersion();
//implementation of the current process
String runningJvmName = bean.getName();
long pid = bean.getPid();//process id
long startTimeMillis = bean.getStartTime();//timestamp when the JVM has started
LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTimeMillis), ZoneId.systemDefault());
long uptime = bean.getUptime();//time the JVM is running (in milliseconds)
List<String> inputArguments = bean.getInputArguments();//arguments provided via the CLI
System.out.printf("""
Running on
%s
from %s
version %s (implementing %s version %s)
since %s (running for %d milliseconds)
with arguments "%s"
on JVM process %s (process id %d)
""", vmName, vendorName, version, specName, specVersion,
String.valueOf(startTime), uptime,
inputArguments.stream().collect(Collectors.joining(" ")),
runningJvmName, pid);
it is a type of MBean that references only a predefined set of data types