温故知新03-ManagementFactory获取JVM以及OS信息

jdk文档上描述ManagementFactory:

ManagementFactory类是用于获取Java平台的受管Bean的工厂类。 该类由静态方法组成,每个方法返回一个或多个表示Java虚拟机组件的管理接口的平台MXBeans 

一、打印系统信息
代码:

public static void main(String[] args) throws Exception {
        ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
        Method method = ManagementFactoryTest.class.getMethod("printSystemInfo");
        method.invoke(managementFactoryTest);
    }

    public void printSystemInfo() {
        System.out.println("打印系统信息-----------");
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        System.out.println("返回最后一分钟的系统负载平均值:" + operatingSystemMXBean.getSystemLoadAverage());
        System.out.println("返回操作系统版本:" + operatingSystemMXBean.getVersion());
        System.out.println("返回操作系统架构:" + operatingSystemMXBean.getArch());
        System.out.println("返回操作系统名称:" + operatingSystemMXBean.getName());
        System.out.println("返回可用于Java虚拟机的处理器数量:" + operatingSystemMXBean.getAvailableProcessors());
    }

输出信息:

打印系统信息-----------
Disconnected from the target VM, address: '127.0.0.1:54855', transport: 'socket'
返回最后一分钟的系统负载平均值:2.84033203125
返回操作系统版本:10.13.6
返回操作系统架构:x86_64
返回操作系统名称:Mac OS X
返回可用于Java虚拟机的处理器数量:4

二、打印Java虚拟机编译系统信息
代码:

public static void main(String[] args) throws Exception {
        ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
        Method method = ManagementFactoryTest.class.getMethod("printCompilationInfo");
        method.invoke(managementFactoryTest);
    }

    public void printCompilationInfo() {
        System.out.println("打印Java虚拟机编译系统信息-----------");
        CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
        System.out.println("返回即时(JIT)编译器的名称:" + compilationMXBean.getName());
        System.out.println("返回在编译中花费的大概累积的经过时间(以毫秒为单位):" + compilationMXBean.getTotalCompilationTime());
        System.out.println("测试Java虚拟机是否支持对编译时间的监视:" + compilationMXBean.isCompilationTimeMonitoringSupported());
    }

运行结果:

打印Java虚拟机编译系统信息-----------
Disconnected from the target VM, address: '127.0.0.1:58858', transport: 'socket'
返回即时(JIT)编译器的名称:HotSpot 64-Bit Tiered Compilers
返回在编译中花费的大概累积的经过时间(以毫秒为单位):41
测试Java虚拟机是否支持对编译时间的监视:true

三、输出内存信息
代码:

public static void main(String[] args) throws Exception {
        ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
        Method method = ManagementFactoryTest.class.getMethod("printMemoryInfo");
        method.invoke(managementFactoryTest);
    }

    public void printMemoryInfo() {
        System.out.println("打印Java内存系统信息-----------");
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        System.out.println("测试是否启用了内存系统的详细输出:" + memoryMXBean.isVerbose());
        System.out.println("返回正在等待完成的对象的大致数量:" + memoryMXBean.getObjectPendingFinalizationCount());
        System.out.println("返回用于对象分配的堆的当前内存使用情况:" + memoryMXBean.getHeapMemoryUsage());
        System.out.println("返回Java虚拟机使用的非堆内存的当前内存使用情况:" + memoryMXBean.getNonHeapMemoryUsage());
    }

运行结果:

打印Java内存系统信息-----------
测试是否启用了内存系统的详细输出:false
返回正在等待完成的对象的大致数量:0
返回用于对象分配的堆的当前内存使用情况:init = 134217728(131072K) used = 5497296(5368K) committed = 128974848(125952K) max = 1908932608(1864192K)
返回Java虚拟机使用的非堆内存的当前内存使用情况:init = 2555904(2496K) used = 4966400(4850K) committed = 8060928(7872K) max = -1(-1K)

四、获取运行时信息
代码:

public static void main(String[] args) throws Exception {
        ManagementFactoryTest managementFactoryTest = new ManagementFactoryTest();
        Method method = ManagementFactoryTest.class.getMethod("printRuntimeInfo");
        method.invoke(managementFactoryTest);
    }

    public void printRuntimeInfo() {
        System.out.println("打印Java运行时信息-----------");
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println("返回bootstrap类加载器用于搜索类文件的引导类路径:" + runtimeMXBean.getBootClassPath());
        System.out.println("返回系统类加载器用于搜索类文件的Java类路径" + runtimeMXBean.getClassPath());
        System.out.println("返回表示正在运行的Java虚拟机的名称" + runtimeMXBean.getName());
        System.out.println("返回Java虚拟机规范名称" + runtimeMXBean.getSpecName());
        System.out.println("返回Java虚拟机规范供应商" + runtimeMXBean.getSpecVendor());
        System.out.println("返回Java虚拟机实现名称" + runtimeMXBean.getVmName());
    }

运行结果:

打印Java运行时信息-----------
返回bootstrap类加载器用于搜索类文件的引导类路径:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/classes:/Users/apple/Library/Caches/IntelliJIdea2018.2/captureAgent/debugger-agent-storage.jar
返回系统类加载器用于搜索类文件的Java类路径/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar:/Users/apple/IdeaProjects/maven_project/cz-parent/cz-consumer/target/classes:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-web/2.1.0.RELEASE/spring-boot-starter-web-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter/2.1.0.RELEASE/spring-boot-starter-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot/2.1.0.RELEASE/spring-boot-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.0.RELEASE/spring-boot-autoconfigure-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-logging/2.1.0.RELEASE/spring-boot-starter-logging-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Applications/Java/Apache/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Applications/Java/Apache/repository/org/apache/logging/log4j/log4j-to-slf4j/2.11.1/log4j-to-slf4j-2.11.1.jar:/Applications/Java/Apache/repository/org/apache/logging/log4j/log4j-api/2.11.1/log4j-api-2.11.1.jar:/Applications/Java/Apache/repository/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Applications/Java/Apache/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Applications/Java/Apache/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-json/2.1.0.RELEASE/spring-boot-starter-json-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-databind/2.9.7/jackson-databind-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/core/jackson-core/2.9.7/jackson-core-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.7/jackson-datatype-jdk8-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.7/jackson-datatype-jsr310-2.9.7.jar:/Applications/Java/Apache/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.7/jackson-module-parameter-names-2.9.7.jar:/Applications/Java/Apache/repository/org/springframework/boot/spring-boot-starter-tomcat/2.1.0.RELEASE/spring-boot-starter-tomcat-2.1.0.RELEASE.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.12/tomcat-embed-core-9.0.12.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.12/tomcat-embed-el-9.0.12.jar:/Applications/Java/Apache/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.12/tomcat-embed-websocket-9.0.12.jar:/Applications/Java/Apache/repository/org/hibernate/validator/hibernate-validator/6.0.13.Final/hibernate-validator-6.0.13.Final.jar:/Applications/Java/Apache/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Applications/Java/Apache/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Applications/Java/Apache/repository/com/fasterxml/classmate/1.4.0/classmate-1.4.0.jar:/Applications/Java/Apache/repository/org/springframework/spring-web/5.1.2.RELEASE/spring-web-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-beans/5.1.2.RELEASE/spring-beans-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-webmvc/5.1.2.RELEASE/spring-webmvc-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-aop/5.1.2.RELEASE/spring-aop-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-context/5.1.2.RELEASE/spring-context-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-expression/5.1.2.RELEASE/spring-expression-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Applications/Java/Apache/repository/org/springframework/spring-core/5.1.2.RELEASE/spring-core-5.1.2.RELEASE.jar:/Applications/Java/Apache/repository/org/springframework/spring-jcl/5.1.2.RELEASE/spring-jcl-5.1.2.RELEASE.jar:/Users/apple/IdeaProjects/maven_project/cz-parent/cz-api/target/classes:/Applications/Java/Apache/repository/com/alibaba/boot/dubbo-spring-boot-starter/0.2.0/dubbo-spring-boot-starter-0.2.0.jar:/Applications/Java/Apache/repository/com/alibaba/dubbo/2.6.2/dubbo-2.6.2.jar:/Applications/Java/Apache/repository/org/javassist/javassist/3.20.0-GA/javassist-3.20.0-GA.jar:/Applications/Java/Apache/repository/org/jboss/netty/netty/3.2.5.Final/netty-3.2.5.Final.jar:/Applications/Java/Apache/repository/org/apache/zookeeper/zookeeper/3.4.9/zookeeper-3.4.9.jar:/Applications/Java/Apache/repository/jline/jline/0.9.94/jline-0.9.94.jar:/Applications/Java/Apache/repository/io/netty/netty/3.10.5.Final/netty-3.10.5.Final.jar:/Applications/Java/Apache/repository/org/apache/curator/curator-framework/2.12.0/curator-framework-2.12.0.jar:/Applications/Java/Apache/repository/org/apache/curator/curator-client/2.12.0/curator-client-2.12.0.jar:/Applications/Java/Apache/repository/com/google/guava/guava/16.0.1/guava-16.0.1.jar:/Applications/Java/Apache/repository/com/alibaba/boot/dubbo-spring-boot-autoconfigure/0.2.0/dubbo-spring-boot-autoconfigure-0.2.0.jar:/Applications/Java/install/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Users/apple/Library/Caches/IntelliJIdea2018.2/captureAgent/debugger-agent.jar
返回表示正在运行的Java虚拟机的名称866@juans.local
返回Java虚拟机规范名称Java Virtual Machine Specification
返回Java虚拟机规范供应商Oracle Corporation
返回Java虚拟机实现名称Java HotSpot(TM) 64-Bit Server VM

五、Dubbo中获取cpu可利用率的示例

public class LoadStatusChecker implements StatusChecker {
    public LoadStatusChecker() {
    }

    public Status check() {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

        double load;
        try {
            Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage");
            load = (Double)method.invoke(operatingSystemMXBean);
        } catch (Throwable var5) {
            load = -1.0D;
        }

        int cpu = operatingSystemMXBean.getAvailableProcessors();
        return new Status(load < 0.0D ? Level.UNKNOWN : (load < (double)cpu ? Level.OK : Level.WARN), (load < 0.0D ? "" : "load:" + load + ",") + "cpu:" + cpu);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容