Java ManagementFactory獲取運行時資訊
一.獲取作業系統資訊
public class o {
public static void main(String[] args) throws Exception {
OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
System.out.println("系統的架構:"+system.getArch());
System.out.println("cpu內核數:"+system.getAvailableProcessors());
System.out.println("系統名稱:"+system.getName());
System.out.println("系統版本:"+system.getVersion());
System.out.println("系統負載平均值:"+system.getSystemLoadAverage());
}
}
系統的架構:amd64
cpu內核數:8
系統名稱:Windows 10
系統版本:10.0
系統負載平均值:-1.0
二.獲取類加載資訊
public class o {
public static void main(String[] args) throws Exception {
ClassLoadingMXBean classLoad= ManagementFactory.getClassLoadingMXBean();
System.out.println("已加載類總數:"+classLoad.getTotalLoadedClassCount());
System.out.println("已加載當前類:"+classLoad.getLoadedClassCount());
System.out.println("已卸載類總數:"+classLoad.getUnloadedClassCount());
}
}
已加載類總數:443
已加載當前類:443
已卸載類總數:0
三.獲取jvm資訊
public static void main(String[] args) throws Exception {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
System.out.println("行程PID="+runtime.getName());
System.out.println("jvm規范名稱:"+runtime.getSpecName());
System.out.println("jvm規范運營商:"+runtime.getSpecVendor());
System.out.println("jvm規范版本:"+runtime.getSpecVersion());
System.out.println("jvm啟動時間(毫秒):"+runtime.getStartTime());
System.out.println("jvm運行時間(毫秒):"+runtime.getUptime());
System.out.println("jvm名稱:"+runtime.getVmName());
System.out.println("jvm運營商:"+runtime.getVmVendor());
System.out.println("jvm實作版本:"+runtime.getVmVersion());
List<String> a = runtime.getInputArguments();
if(a != null && !a.isEmpty()){
System.out.println("vm引數:");
for(String t : a){
System.out.println(t);
}
}
System.out.println("類路徑:"+runtime.getClassPath());
System.out.println("引導類路徑:"+runtime.getBootClassPath());
System.out.println("庫路徑:"+runtime.getLibraryPath());
}
資訊太多你們自己執行看吧
四.獲取記憶體資訊
public static void main(String[] args) throws Exception {
MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
MemoryUsage headMemory = memory.getHeapMemoryUsage();
System.out.println("堆:");
System.out.println("初始:"+headMemory.getInit());
System.out.println("最大"+headMemory.getMax());
System.out.println("已使用"+headMemory.getUsed());
System.out.println("已申請:"+headMemory.getCommitted());
System.out.println("使用率:"+headMemory.getUsed()*1.00/headMemory.getCommitted());
System.out.println("非堆:");
MemoryUsage nonheadMemory = memory.getNonHeapMemoryUsage();
System.out.println("初始:"+nonheadMemory.getInit());
System.out.println("最大"+nonheadMemory.getMax());
System.out.println("已使用:"+nonheadMemory.getUsed());
System.out.println("已申請:"+nonheadMemory.getCommitted());
System.out.println("使用率:"+nonheadMemory.getUsed()*1.00/nonheadMemory.getCommitted());
}
//回傳的是位元組可以自己除以下
初始:266338304
最大3786407936
已使用2663416
已申請:255328256
使用率:0.01043134058770213
非堆:
初始:2555904
最大-1
已使用:4440184
已申請:8060928
使用率:0.5508278947535569
五.獲取執行緒資訊
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
System.out.println("活動的執行緒總數="+thread.getThreadCount());
System.out.println("峰值="+thread.getPeakThreadCount());
System.out.println("創建并執行過的執行緒總數="+thread.getTotalStartedThreadCount());
System.out.println("守護執行緒總數="+thread.getDaemonThreadCount());
//查找死鎖執行緒id
long[] deadlockedIds = thread.findDeadlockedThreads();
if(deadlockedIds != null && deadlockedIds.length > 0){
ThreadInfo[] deadlockInfos = thread.getThreadInfo(deadlockedIds);
System.out.println("死鎖執行緒");
for(ThreadInfo deadlockInfo : deadlockInfos){
System.out.println(deadlockInfo.getThreadName()+"\t"+deadlockInfo.getThreadState()
+"\t"+deadlockInfo.getBlockedTime()+"\t"+deadlockInfo.getWaitedTime()
+"\t"+deadlockInfo.getStackTrace().toString());
}
}
//查找all執行緒id
long[] threadIds = thread.getAllThreadIds();
if(threadIds != null && threadIds.length > 0){
ThreadInfo[] threadInfos = thread.getThreadInfo(threadIds);
for(ThreadInfo threadInfo : threadInfos){
System.out.println("all-名稱"+threadInfo.getThreadName()+",狀態"+threadInfo.getThreadState()
+",id"+threadInfo.getThreadId());
}
}
活動的執行緒總數=5
峰值=5
創建并執行過的執行緒總數=5
守護執行緒總數=4
all-名稱Attach Listener,狀態RUNNABLE,id5
all-名稱Signal Dispatcher,狀態RUNNABLE,id4
all-名稱Finalizer,狀態WAITING,id3
all-名稱Reference Handler,狀態WAITING,id2
all-名稱main,狀態RUNNABLE,id1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/264134.html
標籤:其他
