Java获取 JVM 运行信息

时间:2022-06-08 07:01:45
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale; public class JVMResource{ public static void main(String[] args) {
new JVMResource().printSummary();
} private NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
private NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH)); private final int Kb = ; public void printSummary() {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
System.out.printf("jvmName:%s %s %s%n", runtime.getVmName(), "version", runtime.getVmVersion());
System.out.printf("jvmJavaVer:%s%n", System.getProperty("java.version"));
System.out.printf("jvmVendor:%s%n", runtime.getVmVendor());
System.out.printf("jvmUptime:%s%n", toDuration(runtime.getUptime()));
System.out.printf("threadsLive:%d%n", threads.getThreadCount());
System.out.printf("threadsDaemon:%d%n", threads.getDaemonThreadCount());
System.out.printf("threadsPeak:%d%n", threads.getPeakThreadCount());
System.out.printf("threadsTotal:%d%n", threads.getTotalStartedThreadCount());
System.out.printf("heapCurr:%d%n", mem.getHeapMemoryUsage().getUsed() / Kb);
System.out.printf("heapMax:%d%n", mem.getHeapMemoryUsage().getMax() / Kb);
System.out.printf("heapCommitted:%d%n", mem.getHeapMemoryUsage().getCommitted() / Kb);
System.out.printf("osName:%s %s %s%n", os.getName(), "version", os.getVersion());
System.out.printf("osArch:%s%n", os.getArch());
System.out.printf("osCores:%s%n", os.getAvailableProcessors());
System.out.printf("clsCurrLoaded:%s%n", cl.getLoadedClassCount());
System.out.printf("clsLoaded:%s%n", cl.getTotalLoadedClassCount());
System.out.printf("clsUnloaded:%s%n", cl.getUnloadedClassCount()); } protected String printSizeInKb(double size) {
return fmtI.format((long) (size / )) + " kbytes";
} protected String toDuration(double uptime) {
uptime /= ;
if (uptime < ) {
return fmtD.format(uptime) + " seconds";
}
uptime /= ;
if (uptime < ) {
long minutes = (long) uptime;
String s = fmtI.format(minutes) + (minutes > ? " minutes" : " minute");
return s;
}
uptime /= ;
if (uptime < ) {
long hours = (long) uptime;
long minutes = (long) ((uptime - hours) * );
String s = fmtI.format(hours) + (hours > ? " hours" : " hour");
if (minutes != ) {
s += " " + fmtI.format(minutes) + (minutes > ? " minutes" : " minute");
}
return s;
}
uptime /= ;
long days = (long) uptime;
long hours = (long) ((uptime - days) * );
String s = fmtI.format(days) + (days > ? " days" : " day");
if (hours != ) {
s += " " + fmtI.format(hours) + (hours > ? " hours" : " hour");
}
return s;
} }

相应运行结果如下:

jvmName:Java HotSpot(TM) -Bit Server VM version 25.66-b17
jvmJavaVer:1.8.0_66
jvmVendor:Oracle Corporation
jvmUptime:0.166 seconds
threadsLive:
threadsDaemon:
threadsPeak:
threadsTotal:
heapCurr:
heapMax:
heapCommitted:
osName:Mac OS X version 10.11.
osArch:x86_64
osCores:
clsCurrLoaded:
clsLoaded:
clsUnloaded: