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