java获取JVM和系统的信息

半兽人 发表于: 2018-10-15   最后更新时间: 2018-10-15 23:53:02  
{{totalSubscript}} 订阅, 2,368 游览
package org.dreams.forepart.os;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class JvmInfo {

    static final long MB = 1024 * 1024;

    public static void main(String[] args) {
        //打印系统信息
        System.out.println("===========打印系统信息==========");
        printOperatingSystemInfo();
        //打印编译信息
        System.out.println("===========打印编译信息==========");
        printCompilationInfo();
        //打印类加载信息
        System.out.println("===========打印类加载信息==========");
        printClassLoadingInfo();
        //打印运行时信息
        System.out.println("===========打印运行时信息==========");
        printRuntimeInfo();
        //打印内存管理器信息
        System.out.println("===========打印内存管理器信息==========");
        printMemoryManagerInfo();
        //打印垃圾回收信息
        System.out.println("===========打印垃圾回收信息==========");
        printGarbageCollectorInfo();
        //打印vm内存
        System.out.println("===========打印vm内存信息==========");
        printMemoryInfo();
        //打印vm各内存区信息
        System.out.println("===========打印vm各内存区信息==========");
        printMemoryPoolInfo();
        //打印线程信息
        System.out.println("===========打印线程==========");
        printThreadInfo();

    }


    private static void printOperatingSystemInfo(){
        OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
        //相当于System.getProperty("os.name").
        System.out.println("系统名称:"+system.getName());
        //相当于System.getProperty("os.version").
        System.out.println("系统版本:"+system.getVersion());
        //相当于System.getProperty("os.arch").
        System.out.println("操作系统的架构:"+system.getArch());
        //相当于 Runtime.availableProcessors()
        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);
        }
    }

    private static void printCompilationInfo(){
        CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
        System.out.println("JIT编译器名称:"+compilation.getName());
        //判断jvm是否支持编译时间的监控
        if(compilation.isCompilationTimeMonitoringSupported()){
            System.out.println("总编译时间:"+compilation.getTotalCompilationTime()+"秒");
        }
    }

    private static void printClassLoadingInfo(){
        ClassLoadingMXBean classLoad= ManagementFactory.getClassLoadingMXBean();
        System.out.println("已加载类总数:"+classLoad.getTotalLoadedClassCount());
        System.out.println("已加载当前类:"+classLoad.getLoadedClassCount());
        System.out.println("已卸载类总数:"+classLoad.getUnloadedClassCount());

    }

    private 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.getProperties
        System.out.println("获取System.properties:"+runtime.getSystemProperties());
        System.out.println("jvm正常运行时间(毫秒):"+runtime.getUptime());
        //相当于System.getProperty("java.vm.name").
        System.out.println("jvm名称:"+runtime.getVmName());
        //相当于System.getProperty("java.vm.vendor").
        System.out.println("jvm运营商:"+runtime.getVmVendor());
        //相当于System.getProperty("java.vm.version").
        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());
    }

    private 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());
            }
        }
    }

    private 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()));
        }
    }

    private 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()+"%");
    }

    private static void printMemoryPoolInfo(){
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
        if(pools != null && !pools.isEmpty()){
            for(MemoryPoolMXBean pool : pools){
                //只打印一些各个内存区都有的属性,一些区的特殊属性,可看文档或百度
                //最大值,初始值,如果没有定义的话,返回-1,所以真正使用时,要注意
                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())+"%");

            }
        }
    }

    private 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);
    }
}

结果

===========打印系统信息==========
系统名称:Windows 7
系统版本:6.1
操作系统的架构:amd64
可用的内核数:4
总物理内存(M):6041
已用物理内存(M):5387
剩余物理内存(M):653
总交换空间(M):18136
已用交换空间(M):15817
剩余交换空间(M):2319
===========打印编译信息==========
JIT编译器名称:HotSpot 64-Bit Tiered Compilers
总编译时间:0秒
===========打印类加载信息==========
已加载类总数:410
已加载当前类:410
已卸载类总数:0
===========打印运行时信息==========
进程PID=386220
jvm规范名称:Java Virtual Machine Specification
jvm规范运营商:Oracle Corporation
jvm规范版本:1.7
jvm启动时间(毫秒):1447134405258
获取System.properties:{java.vm.version=24.80-b11, sun.jnu.encoding=GBK, java.vendor.url=https://java.oracle.com/, java.vm.info=mixed mode, user.dir=E:\test2\test200, sun.cpu.isalist=amd64, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, sun.os.patch.level=Service Pack 1, java.io.tmpdir=C:\Users\WEIJIA~1\AppData\Local\Temp\, user.home=E:\USER\weijianfei, java.awt.printerjob=sun.awt.windows.WPrinterJob, java.version=1.7.0_80, file.encoding.pkg=sun.io, java.vendor.url.bug=https://bugreport.sun.com/bugreport/, file.encoding=UTF-8, line.separator=
, sun.java.command=com.fei.JvmInfo, java.vm.specification.vendor=Oracle Corporation, java.vm.vendor=Oracle Corporation, java.class.path=E:\test2\test200\target\classes;E:\setup\maven\org\apache\zookeeper\zookeeper\3.4.6\zookeeper-3.4.6.jar;E:\setup\maven\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar;E:\setup\maven\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;E:\setup\maven\log4j\log4j\1.2.16\log4j-1.2.16.jar;E:\setup\maven\jline\jline\0.9.94\jline-0.9.94.jar;E:\setup\maven\junit\junit\3.8.1\junit-3.8.1.jar;E:\setup\maven\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;E:\setup\maven\org\springframework\spring-webmvc\4.2.1.RELEASE\spring-webmvc-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-beans\4.2.1.RELEASE\spring-beans-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-context\4.2.1.RELEASE\spring-context-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-aop\4.2.1.RELEASE\spring-aop-4.2.1.RELEASE.jar;E:\setup\maven\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\setup\maven\org\springframework\spring-core\4.2.1.RELEASE\spring-core-4.2.1.RELEASE.jar;E:\setup\maven\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\setup\maven\org\springframework\spring-expression\4.2.1.RELEASE\spring-expression-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-web\4.2.1.RELEASE\spring-web-4.2.1.RELEASE.jar, sun.io.unicode.encoding=UnicodeLittle, user.variant=, os.arch=amd64, user.name=weijianfei, user.language=zh, java.runtime.version=1.7.0_80-b15, sun.boot.class.path=D:\jre1.7\lib\resources.jar;D:\jre1.7\lib\rt.jar;D:\jre1.7\lib\sunrsasign.jar;D:\jre1.7\lib\jsse.jar;D:\jre1.7\lib\jce.jar;D:\jre1.7\lib\charsets.jar;D:\jre1.7\lib\jfr.jar;D:\jre1.7\classes, sun.desktop=windows, sun.cpu.endian=little, awt.toolkit=sun.awt.windows.WToolkit, sun.boot.library.path=D:\jre1.7\bin, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, java.home=D:\jre1.7, java.endorsed.dirs=D:\jre1.7\lib\endorsed, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.runtime.name=Java(TM) SE Runtime Environment, java.library.path=D:\jre1.7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.6.0_45\bin;C:\Program Files\Java\jdk1.6.0_45\jre\bin;D:\soft\svn1.8\bin;D:\apache-maven-3.2.3\bin;D:\mysql5.7\MySQL Server 5.7\bin;D:\mysql5.7\MySQL Utilities 1.3.5\;D:\mongoDB;., file.separator=\, java.specification.vendor=Oracle Corporation, java.vm.specification.version=1.7, sun.java.launcher=SUN_STANDARD, user.timezone=, os.name=Windows 7, path.separator=;, java.ext.dirs=D:\jre1.7\lib\ext;C:\Windows\Sun\Java\lib\ext, sun.arch.data.model=64, java.specification.name=Java Platform API Specification, os.version=6.1, user.script=, user.country=CN, java.class.version=51.0, java.vendor=Oracle Corporation, java.vm.specification.name=Java Virtual Machine Specification, java.specification.version=1.7}
jvm正常运行时间(毫秒):573
jvm名称:Java HotSpot(TM) 64-Bit Server VM
jvm运营商:Oracle Corporation
jvm实现版本:24.80-b11
vm参数:
-Dfile.encoding=UTF-8
类路径:E:\test2\test200\target\classes;E:\setup\maven\org\apache\zookeeper\zookeeper\3.4.6\zookeeper-3.4.6.jar;E:\setup\maven\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar;E:\setup\maven\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;E:\setup\maven\log4j\log4j\1.2.16\log4j-1.2.16.jar;E:\setup\maven\jline\jline\0.9.94\jline-0.9.94.jar;E:\setup\maven\junit\junit\3.8.1\junit-3.8.1.jar;E:\setup\maven\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;E:\setup\maven\org\springframework\spring-webmvc\4.2.1.RELEASE\spring-webmvc-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-beans\4.2.1.RELEASE\spring-beans-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-context\4.2.1.RELEASE\spring-context-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-aop\4.2.1.RELEASE\spring-aop-4.2.1.RELEASE.jar;E:\setup\maven\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;E:\setup\maven\org\springframework\spring-core\4.2.1.RELEASE\spring-core-4.2.1.RELEASE.jar;E:\setup\maven\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;E:\setup\maven\org\springframework\spring-expression\4.2.1.RELEASE\spring-expression-4.2.1.RELEASE.jar;E:\setup\maven\org\springframework\spring-web\4.2.1.RELEASE\spring-web-4.2.1.RELEASE.jar
引导类路径:D:\jre1.7\lib\resources.jar;D:\jre1.7\lib\rt.jar;D:\jre1.7\lib\sunrsasign.jar;D:\jre1.7\lib\jsse.jar;D:\jre1.7\lib\jce.jar;D:\jre1.7\lib\charsets.jar;D:\jre1.7\lib\jfr.jar;D:\jre1.7\classes
库路径:D:\jre1.7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.6.0_45\bin;C:\Program Files\Java\jdk1.6.0_45\jre\bin;D:\soft\svn1.8\bin;D:\apache-maven-3.2.3\bin;D:\mysql5.7\MySQL Server 5.7\bin;D:\mysql5.7\MySQL Utilities 1.3.5\;D:\mongoDB;.
===========打印内存管理器信息==========
vm内存管理器:名称=CodeCacheManager,管理的内存区=[Code Cache],ObjectName=java.lang:type=MemoryManager,name=CodeCacheManager
vm内存管理器:名称=PS Scavenge,管理的内存区=[PS Eden Space, PS Survivor Space],ObjectName=java.lang:type=GarbageCollector,name=PS Scavenge
vm内存管理器:名称=PS MarkSweep,管理的内存区=[PS Eden Space, PS Survivor Space, PS Old Gen, PS Perm Gen],ObjectName=java.lang:type=GarbageCollector,name=PS MarkSweep
===========打印垃圾回收信息==========
垃圾收集器:名称=PS Scavenge,收集=0,总花费时间=0,内存区名称=[PS Eden Space, PS Survivor Space]
垃圾收集器:名称=PS MarkSweep,收集=0,总花费时间=0,内存区名称=[PS Eden Space, PS Survivor Space, PS Old Gen, PS Perm Gen]
===========打印vm内存信息==========
head堆:
    初始(M):94
    最大(上限)(M):1344
    当前(已使用)(M):1
    提交的内存(已申请)(M):91
    使用率:1%
non-head非堆:
    初始(M):23
    最大(上限)(M):130
    当前(已使用)(M):3
    提交的内存(已申请)(M):23
    使用率:13%
===========打印vm各内存区信息==========
vm内存区:
    名称=Code Cache
    所属内存管理者=[CodeCacheManager]
     ObjectName=java.lang:type=MemoryPool,name=Code Cache
    初始大小(M)=2
    最大(上限)(M)=48
    已用大小(M)=0
    已提交(已申请)(M)=2
    使用率=15%
vm内存区:
    名称=PS Eden Space
    所属内存管理者=[PS MarkSweep, PS Scavenge]
     ObjectName=java.lang:type=MemoryPool,name=PS Eden Space
    初始大小(M)=24
    最大(上限)(M)=497
    已用大小(M)=1
    已提交(已申请)(M)=24
    使用率=6%
vm内存区:
    名称=PS Survivor Space
    所属内存管理者=[PS MarkSweep, PS Scavenge]
     ObjectName=java.lang:type=MemoryPool,name=PS Survivor Space
    初始大小(M)=3
    最大(上限)(M)=3
    已用大小(M)=0
    已提交(已申请)(M)=3
    使用率=0%
vm内存区:
    名称=PS Old Gen
    所属内存管理者=[PS MarkSweep]
     ObjectName=java.lang:type=MemoryPool,name=PS Old Gen
    初始大小(M)=63
    最大(上限)(M)=1008
    已用大小(M)=0
    已提交(已申请)(M)=63
    使用率=0%
vm内存区:
    名称=PS Perm Gen
    所属内存管理者=[PS MarkSweep]
     ObjectName=java.lang:type=MemoryPool,name=PS Perm Gen
    初始大小(M)=21
    最大(上限)(M)=82
    已用大小(M)=2
    已提交(已申请)(M)=21
    使用率=13%
===========打印线程==========
ObjectName=java.lang:type=Threading
仍活动的线程总数=5
峰值=5
线程总数(被创建并执行过的线程总数)=5
当初仍活动的守护线程(daemonThread)总数=4
所有线程信息:
        线程名称                    状态                    线程id
        Attach Listener                    RUNNABLE                    5
        Signal Dispatcher                    RUNNABLE                    4
        Finalizer                    WAITING                    3
        Reference Handler                    WAITING                    2
        main                    RUNNABLE                    1



查看java更多相关的文章或提一个关于java的问题,也可以与我们一起分享文章
提问