Week 24 — What is the `RuntimeMXBean? in Java?

Question of the Week #24
What is the `RuntimeMXBean? in Java?
3 Replies
Eric McIntyre
Eric McIntyre2y ago
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);
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);
Submission from dan1st#7327
Eric McIntyre
Eric McIntyre2y ago
it is a type of MBean that references only a predefined set of data types
Submission from Denis Z.#0177
Eric McIntyre
Eric McIntyre2y ago
Like the Runtime class, the RuntimeMXBean provides information about the currently running vm. Unlike the Runtime class tho, it doesn’t provide any way to change or interact with the runtime, since it’s a bean. It does provide more information than the Runtime class, but does so in a way that can be easily imitated with System.getProperty and other related methods. It’s a good thing to use to ensure cross runtime version compatibility though. You can obtain a RuntimeMXBean with ManagementFactory’s getRuntimeMXBean method. There are other beans providing info about other related components of the jvm as well, such as the MemoryMXBean, CompilationMXBean, ThreadMXBean, OperatingSystemMXBean, etc. Again, all of these beans only provide information, and don’t allow you to change or manipulate their respective components.
⭐ Submission from 0x150#9699
Want results from more Discord servers?
Add your server