监控JVM信息的工具有JConsole。而ManagementFactory是一个可以获取JVM线程、内存、编译等信息的一个工厂类。

ManagementFactory使用:
public class JvmInfo {
private static final long MB = 1024 * 1024;
public static void printAll() {
System.out.println("===========打印系统信息==========");
printOperatingSystemInfo();
System.out.println("===========打印编译信息==========");
printCompilationInfo();
System.out.println("===========打印类加载信息==========");
printClassLoadingInfo();
System.out.println("===========打印运行时信息==========");
printRuntimeInfo();
System.out.println("===========打印内存管理器信息==========");
printMemoryManagerInfo();
System.out.println("===========打印垃圾回收信息==========");
printGarbageCollectorInfo();
System.out.println("===========打印vm内存信息==========");
printMemoryInfo();
System.out.println("===========打印vm各内存区信息==========");
printMemoryPoolInfo();
System.out.println("===========打印线程==========");
printThreadInfo();
}
public static void printOperatingSystemInfo(){
OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
System.out.println("系统名称:"+system.getName());
System.out.println("系统版本:"+system.getVersion());
System.out.println("操作系统的架构:"+system.getArch());
System.out.println("可用的内核数:"+system.getAvailableProcessors());
if(isSunOsMBean(system)){
long totalPhysicalMemory = getLongFromOperatingSystem(system,"getTotalPhysicalMemorySize");
long freePhysicalMemory = getLongFromOperatingSystem(system, "getFreePhysicalMemorySize");
long usedPhysicalMemorySize =totalPhysicalMemory - freePhysicalMemory;
System.out.println("总物理内存(M):"+totalPhysicalMemory/MB);
System.out.println("已用物理内存(M):"+usedPhysicalMemorySize/MB);
System.out.println("剩余物理内存(M):"+freePhysicalMemory/MB);
long totalSwapSpaceSize = getLongFromOperatingSystem(system, "getTotalSwapSpaceSize");
long freeSwapSpaceSize = getLongFromOperatingSystem(system, "getFreeSwapSpaceSize");
long usedSwapSpaceSize = totalSwapSpaceSize - freeSwapSpaceSize;
System.out.println("总交换空间(M):"+totalSwapSpaceSize/MB);
System.out.println("已用交换空间(M):"+usedSwapSpaceSize/MB);
System.out.println("剩余交换空间(M):"+freeSwapSpaceSize/MB);
}
}
private static long getLongFromOperatingSystem(OperatingSystemMXBean operatingSystem, String methodName) {
try {
final Method method = operatingSystem.getClass().getMethod(methodName,
(Class<?>[]) null);
method.setAccessible(true);
return (Long) method.invoke(operatingSystem, (Object[]) null);
} catch (final InvocationTargetException e) {
if (e.getCause() instanceof Error) {
throw (Error) e.getCause();
} else if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new IllegalStateException(e.getCause());
} catch (final NoSuchMethodException e) {
throw new IllegalArgumentException(e);
} catch (final IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
public static void printCompilationInfo(){
CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
System.out.println("JIT编译器名称:"+compilation.getName());
if(compilation.isCompilationTimeMonitoringSupported()){
System.out.println("总编译时间:"+compilation.getTotalCompilationTime()+"秒");
}
}
public static void printClassLoadingInfo(){
ClassLoadingMXBean classLoad= ManagementFactory.getClassLoadingMXBean();
System.out.println("已加载类总数:"+classLoad.getTotalLoadedClassCount());
System.out.println("已加载当前类:"+classLoad.getLoadedClassCount());
System.out.println("已卸载类总数:"+classLoad.getUnloadedClassCount());
}
public static void printRuntimeInfo(){
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
System.out.println("进程PID="+runtime.getName().split("@")[0]);
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("获取System.properties:"+runtime.getSystemProperties());
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> args = runtime.getInputArguments();
if(args != null && !args.isEmpty()){
System.out.println("vm参数:");
for(String arg : args){
System.out.println(arg);
}
}
System.out.println("类路径:"+runtime.getClassPath());
System.out.println("引导类路径:"+runtime.getBootClassPath());
System.out.println("库路径:"+runtime.getLibraryPath());
}
public static void printMemoryManagerInfo(){
List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans();
if(managers != null && !managers.isEmpty()){
for(MemoryManagerMXBean manager : managers){
System.out.println("vm内存管理器:名称="+manager.getName()+",管理的内存区="
+Arrays.deepToString(manager.getMemoryPoolNames())+",ObjectName="+manager.getObjectName());
}
}
}
public static void printGarbageCollectorInfo(){
List<GarbageCollectorMXBean> garbages = ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean garbage : garbages){
System.out.println("垃圾收集器:名称="+garbage.getName()+",收集="+garbage.getCollectionCount()+",总花费时间="
+garbage.getCollectionTime()+",内存区名称="+Arrays.deepToString(garbage.getMemoryPoolNames()));
}
}
public static void printMemoryInfo(){
MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
MemoryUsage headMemory = memory.getHeapMemoryUsage();
System.out.println("head堆:");
System.out.println("\t初始(M):"+headMemory.getInit()/MB);
System.out.println("\t最大(上限)(M):"+headMemory.getMax()/MB);
System.out.println("\t当前(已使用)(M):"+headMemory.getUsed()/MB);
System.out.println("\t提交的内存(已申请)(M):"+headMemory.getCommitted()/MB);
System.out.println("\t使用率:"+headMemory.getUsed()*100/headMemory.getCommitted()+"%");
System.out.println("non-head非堆:");
MemoryUsage nonheadMemory = memory.getNonHeapMemoryUsage();
System.out.println("\t初始(M):"+nonheadMemory.getInit()/MB);
System.out.println("\t最大(上限)(M):"+nonheadMemory.getMax()/MB);
System.out.println("\t当前(已使用)(M):"+nonheadMemory.getUsed()/MB);
System.out.println("\t提交的内存(已申请)(M):"+nonheadMemory.getCommitted()/MB);
System.out.println("\t使用率:"+nonheadMemory.getUsed()*100/nonheadMemory.getCommitted()+"%");
}
public static void printMemoryPoolInfo(){
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
if(pools != null && !pools.isEmpty()){
for(MemoryPoolMXBean pool : pools){
System.out.println("vm内存区:\n\t名称="+pool.getName()+"\n\t所属内存管理者="+Arrays.deepToString(pool.getMemoryManagerNames())
+"\n\t ObjectName="+pool.getObjectName()+"\n\t初始大小(M)="+pool.getUsage().getInit()/MB
+"\n\t最大(上限)(M)="+pool.getUsage().getMax()/MB
+"\n\t已用大小(M)="+pool.getUsage().getUsed()/MB
+"\n\t已提交(已申请)(M)="+pool.getUsage().getCommitted()/MB
+"\n\t使用率="+(pool.getUsage().getUsed()*100/pool.getUsage().getCommitted())+"%");
}
}
}
public static void printThreadInfo(){
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
System.out.println("ObjectName="+thread.getObjectName());
System.out.println("仍活动的线程总数="+thread.getThreadCount());
System.out.println("峰值="+thread.getPeakThreadCount());
System.out.println("线程总数(被创建并执行过的线程总数)="+thread.getTotalStartedThreadCount());
System.out.println("当初仍活动的守护线程(daemonThread)总数="+thread.getDaemonThreadCount());
long[] deadlockedIds = thread.findDeadlockedThreads();
if(deadlockedIds != null && deadlockedIds.length > 0){
ThreadInfo[] deadlockInfos = thread.getThreadInfo(deadlockedIds);
System.out.println("死锁线程信息:");
System.out.println("\t\t线程名称\t\t状态\t\t");
for(ThreadInfo deadlockInfo : deadlockInfos){
System.out.println("\t\t"+deadlockInfo.getThreadName()+"\t\t"+deadlockInfo.getThreadState()
+"\t\t"+deadlockInfo.getBlockedTime()+"\t\t"+deadlockInfo.getWaitedTime()
+"\t\t"+deadlockInfo.getStackTrace().toString());
}
}
long[] threadIds = thread.getAllThreadIds();
if(threadIds != null && threadIds.length > 0){
ThreadInfo[] threadInfos = thread.getThreadInfo(threadIds);
System.out.println("所有线程信息:");
System.out.println("\t\t线程名称\t\t\t\t\t状态\t\t\t\t\t线程id");
for(ThreadInfo threadInfo : threadInfos){
System.out.println("\t\t"+threadInfo.getThreadName()+"\t\t\t\t\t"+threadInfo.getThreadState()
+"\t\t\t\t\t"+threadInfo.getThreadId());
}
}
}
private static boolean isSunOsMBean(OperatingSystemMXBean operatingSystem) {
final String className = operatingSystem.getClass().getName();
return "com.sun.management.OperatingSystem".equals(className)
|| "com.sun.management.UnixOperatingSystem".equals(className);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2018-04-13 Java内存模型FAQ(一) 什么是内存模型