JAVA獲取jvm和作業系統相關資訊
背景
今日搬磚??時需要獲取系統運行時間、版本號等相關資訊,使用Java自帶的類進行獲取系統運行的相關資訊,在這整理記錄分享一下,感興趣的小伙伴可以自己嘗試嘗試,
Jvm
首先獲取jvm相關資訊,包含jvm的名稱、版本號、啟動時間、運行時間、環境變數、行程id等等
public class Test {
public static void main(String[] args) {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
// jvmName
System.out.printf("jvmName: %s %n", runtimeMXBean.getVmName());
// jvmVersion
System.out.printf("jvmVersion: %s %n", runtimeMXBean.getVmVersion());
// jvmVendor
System.out.printf("jvmVendor: %s %n", runtimeMXBean.getVmVendor());
// startTime 使用hutool中DateUtil進行轉換
long startTime = runtimeMXBean.getStartTime();
System.out.printf("startTime: %s %n", DateUtil.date(startTime).toString());
// updateTime
long uptime = runtimeMXBean.getUptime();
System.out.printf("updateTime: %s %n", DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND));
// classPath
System.out.printf("classPath: %s %n", runtimeMXBean.getClassPath());
// systemProperties
System.out.printf("jvmName: %s %n", runtimeMXBean.getSystemProperties());
// bootClassPath
System.out.printf("bootClassPath: %s %n", runtimeMXBean.getBootClassPath());
// processId
System.out.printf("processId: %s %n", runtimeMXBean.getName().split("@")[0]);
}
}
還可以獲取JVM記憶體相關資訊,例如堆記憶體,
public class Test {
public static void main(String[] args) {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
// heapMemoryUsed
System.out.printf("heapMemoryUsed: %d MB %n", heapMemoryUsage.getUsed()/1024/1024);
// heapMemoryMax
System.out.printf("heapMemoryMax: %d MB %n", heapMemoryUsage.getMax()/1024/1024);
// heapMemoryCommitted
System.out.printf("heapMemoryCommitted: %d MB %n", heapMemoryUsage.getCommitted()/1024/1024);
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
// nonHeapMemoryUsed
System.out.printf("nonHeapMemoryUsed: %d MB %n", nonHeapMemoryUsage.getUsed()/1024/1024);
// nonHeapMemoryMax
System.out.printf("nonHeapMemoryMax: %d MB %n", nonHeapMemoryUsage.getMax()/1024/1024);
// nonHeapMemoryCommitted
System.out.printf("nonHeapMemoryCommitted: %d MB %n", nonHeapMemoryUsage.getCommitted()/1024/1024);
}
}
獲取JDK相關資訊,包含jdk的版本、安裝路徑、當前運行jar包路徑、運行jar檔案名等,
public class Test {
public static void main(String[] args) {
// jdkVersion
System.out.printf("jdkVersion: %s %n", System.getProperty("java.version"));
// java_home
System.out.printf("java_home: %s %n", System.getProperty("java.home"));
// jar包路徑
String path = System.getProperty("java.class.path");
int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
int lastIndex = path.lastIndexOf(File.separator) + 1;
String jarPath = path.substring(firstIndex, lastIndex);
System.out.printf("jarPath: %s %n", jarPath);
// 當前運行jar檔案名
String jarName = path.substring(lastIndex);
System.out.printf("jarName: %s %n", jarName);
}
}
獲取java虛擬機執行緒資訊,包含執行緒的阻塞時間、次數、執行緒的堆疊資訊等等,
public class Test {
public static void main(String[] args) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
// ThreadCount
System.out.printf("ThreadCount: %d %n", threadMXBean.getThreadCount());
// AllThreadIds
System.out.printf("AllThreadIds: %s %n", threadMXBean.getAllThreadIds());
// TotalStartedThread
System.out.printf("TotalStartedThread: %d %n", threadMXBean.getTotalStartedThreadCount());
// DaemonThread
System.out.printf("DaemonThread: %d %n", threadMXBean.getDaemonThreadCount());
// PeakThread
System.out.printf("PeakThread: %d %n", threadMXBean.getPeakThreadCount());
// ThreadInfo
System.out.printf("ThreadInfo: %s %n", threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()));
}
}
獲取java虛擬機類加載相關資訊,
public class Test {
public static void main(String[] args) {
ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
// LoadedClassCount
System.out.printf("LoadedClassCount: %d %n", classLoadingMXBean.getLoadedClassCount());
// TotalLoadedClassCount
System.out.printf("TotalLoadedClassCount: %d %n", classLoadingMXBean.getTotalLoadedClassCount());
// UnloadedClassCount
System.out.printf("UnloadedClassCount: %d %n", classLoadingMXBean.getUnloadedClassCount());
}
}
作業系統
獲取作業系統以及主機硬體資訊,包含系統名稱、版本、物理記憶體、可用記憶體等等,
public class Test {
public static void main(String[] args) {
// 系統版本
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean();
// osName
System.out.printf("osName: %s %n", operatingSystemMXBean.getName());
// Arch
System.out.printf("Arch: %s %n", operatingSystemMXBean.getArch());
// Version
System.out.printf("Version: %s %n", operatingSystemMXBean.getVersion());
// 物理記憶體
long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize()/1024/1024/1024;
// totalPhysicalMemorySize
System.out.printf("totalPhysicalMemorySize: %d GB %n", totalPhysicalMemorySize);
}
}
原文鏈接:https://monkey.blog.xpyvip.top/archives/java-huo-qu-jvm-he-cao-zuo-xi-tong-xiang-guan-xin-xi
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517504.html
標籤:Java
