Operating System MXBean Load
Read host details from the OperatingSystemMXBean: name, arch, processors, and a load average that may be unavailable as -1.
Code
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
public class Main {
public static void main(String[] args) {
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
// these values describe the JVM host, not container limits
System.out.println("name: " + os.getName());
System.out.println("arch: " + os.getArch());
System.out.println("version: " + os.getVersion());
System.out.println("processors: " + os.getAvailableProcessors());
System.out.println("load average: " + os.getSystemLoadAverage());
// getSystemLoadAverage returns -1 when the platform cannot report it
double load = os.getSystemLoadAverage();
System.out.println("load known: " + (load >= 0));
// ObjectName identifies the bean in the platform MBean server
System.out.println("object name: " + os.getObjectName());
}
}How it works
Operating System MXBean Load is built around the class Main and the function main.
Caveats
- No external surface: all behavior comes from the standard library.
Putting it to use
Entry signature: public static void main(String[] args)
The printed trace comes labeled — name:, arch:, version: — when the file runs.
Core lines
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
double load = os.getSystemLoadAverage();
FAQ
- What does Operating System MXBean Load print when run?
- Invoke the file as-is — the `name:`, `arch:`, `version:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.