Java擷取 JVM 運行資訊

來源:互聯網
上載者:User

標籤:

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;public class JVMResource{    public static void main(String[] args) {        new JVMResource().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;    }}

相應運行結果如下:

jvmName:Java HotSpot(TM) 64-Bit Server VM version 25.66-b17jvmJavaVer:1.8.0_66jvmVendor:Oracle CorporationjvmUptime:0.166 secondsthreadsLive:4threadsDaemon:3threadsPeak:4threadsTotal:4heapCurr:2662heapMax:1864192heapCommitted:125952osName:Mac OS X version 10.11.1osArch:x86_64osCores:4clsCurrLoaded:585clsLoaded:585clsUnloaded:0

 

Java擷取 JVM 運行資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.