Java 实践:获取 一些 JVM 运行信息

简介: 上代码 /** * The MIT License * * Copyright (c) 2016 Shell Technologies PTY LTD. http://au.shellpays.com * * You may obtain a copy of the Lice

上代码

/**
 * The MIT License
 * 
 * Copyright (c) 2016 Shell Technologies PTY LTD. http://au.shellpays.com
 *
 * You may obtain a copy of the License at
 * 
 *       http://mit-license.org/
 *       
 */
package com.shellpays.literminal.ux;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author Steven Chen
 *
 */
public class Test {

    public static void main(String[] args) {
        new Test().printSummary();
    }

    private NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
    private NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));

    private final int Kb = 1024;

    public void printSummary() {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
        System.out.printf("jvmName:%s %s %s%n", runtime.getVmName(), "version", runtime.getVmVersion());
        System.out.printf("jvmJavaVer:%s%n", System.getProperty("java.version"));
        System.out.printf("jvmVendor:%s%n", runtime.getVmVendor());
        System.out.printf("jvmUptime:%s%n", toDuration(runtime.getUptime()));
        System.out.printf("threadsLive:%d%n", threads.getThreadCount());
        System.out.printf("threadsDaemon:%d%n", threads.getDaemonThreadCount());
        System.out.printf("threadsPeak:%d%n", threads.getPeakThreadCount());
        System.out.printf("threadsTotal:%d%n", threads.getTotalStartedThreadCount());
        System.out.printf("heapCurr:%d%n", mem.getHeapMemoryUsage().getUsed() / Kb);
        System.out.printf("heapMax:%d%n", mem.getHeapMemoryUsage().getMax() / Kb);
        System.out.printf("heapCommitted:%d%n", mem.getHeapMemoryUsage().getCommitted() / Kb);
        System.out.printf("osName:%s %s %s%n", os.getName(), "version", os.getVersion());
        System.out.printf("osArch:%s%n", os.getArch());
        System.out.printf("osCores:%s%n", os.getAvailableProcessors());
        System.out.printf("clsCurrLoaded:%s%n", cl.getLoadedClassCount());
        System.out.printf("clsLoaded:%s%n", cl.getTotalLoadedClassCount());
        System.out.printf("clsUnloaded:%s%n", cl.getUnloadedClassCount());

    }

    protected String printSizeInKb(double size) {
        return fmtI.format((long) (size / 1024)) + " kbytes";
    }

    protected String toDuration(double uptime) {
        uptime /= 1000;
        if (uptime < 60) {
            return fmtD.format(uptime) + " seconds";
        }
        uptime /= 60;
        if (uptime < 60) {
            long minutes = (long) uptime;
            String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            return s;
        }
        uptime /= 60;
        if (uptime < 24) {
            long hours = (long) uptime;
            long minutes = (long) ((uptime - hours) * 60);
            String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
            if (minutes != 0) {
                s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
            }
            return s;
        }
        uptime /= 24;
        long days = (long) uptime;
        long hours = (long) ((uptime - days) * 24);
        String s = fmtI.format(days) + (days > 1 ? " days" : " day");
        if (hours != 0) {
            s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
        }
        return s;
    }

}

The print result

jvmName:Java HotSpot(TM) 64-Bit Server VM version 25.66-b17
jvmJavaVer:1.8.0_66
jvmVendor:Oracle Corporation
jvmUptime:0.166 seconds
threadsLive:4
threadsDaemon:3
threadsPeak:4
threadsTotal:4
heapCurr:2662
heapMax:1864192
heapCommitted:125952
osName:Mac OS X version 10.11.1
osArch:x86_64
osCores:4
clsCurrLoaded:585
clsLoaded:585
clsUnloaded:0
目录
相关文章
|
19天前
|
XML Java 数据库连接
解码Java SPI:深入理解与实践【七】
解码Java SPI:深入理解与实践【七】
15 0
|
5天前
|
存储 算法 Java
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
33 1
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
|
1天前
|
IDE Java 应用服务中间件
Java Web开发入门指南:从基础到实践
Java Web开发入门指南:从基础到实践
|
1天前
|
存储 安全 算法
Java泛型与集合:类型安全的集合操作实践
Java泛型与集合:类型安全的集合操作实践
|
1天前
|
IDE Java 测试技术
使用Java进行单元测试:实践与技巧
使用Java进行单元测试:实践与技巧
|
1天前
|
安全 Java 程序员
Java泛型编程:技巧与实践
Java泛型编程:技巧与实践
|
1天前
|
存储 监控 Java
JVM监控和分析技术在实践中可能会面临什么?
JVM监控和分析技术在实践中可能会面临什么?
|
1天前
|
运维 监控 Java
Java在运维领域的技术应用与实践
Java在运维领域的技术应用与实践
|
1天前
|
运维 监控 Java
Java在运维领域的技术应用与实践
Java在运维领域的技术应用与实践
|
1天前
|
监控 算法 Java
百度搜索:蓝易云【JAVA系列之JVM内存调优】
以上策略需要根据具体的应用场景和需求来进行调优。调优时建议先进行性能测试和分析,再根据测试结果来选择合适的参数配置,以获得最佳的性能和稳定性。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
138 3

相关产品

  • 云迁移中心