Java获取 JVM 运行信息

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-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
时间: 2024-10-12 20:13:09

Java获取 JVM 运行信息的相关文章

java获取天气预报的信息

运行效果: 主要功能: 1,jsp页面输入省份和城市 根据条件获取当地的天气信息 2,java代码 利用第三方的省份和城市的路径地址 本工程主要实现java获取天气预报的信息步骤1,创建工程weatherDemo2,创建包结构3,创建类4,访问第三方接口 打开主机方法5,获取省份id方法6,获取市id方法7,获取天气的方法8,编写servlet9,发布运行 java代码 创建WeatherDemo类 /** * @version 1.0 * @author ren * 天气预报的核心接口 * *

java获取当前操作系统的信息

java获取当前操作系统的信息 JavaOS虚拟机UnixEXT 从网上收集的一些关于java获取操作系统信息的方法,现在总结一下: 1获取本机的IP地址: Java代码   private static String getIpAddress() throws UnknownHostException { InetAddress address = InetAddress.getLocalHost(); return address.getHostAddress(); } 2获得网卡地址 Ja

使用psutil模块获取电脑运行信息

psutil是python的一个用于获取cpu信息的模块,非常好使,以下附上官方的一些example: CPU-> Examples ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 >>> import psutil >>> psutil.cpu_times() scputimes(user=3961.46, nice=169.729, sy

JAVA和JVM运行原理揭秘

这里和大家简单分享一下JAVA和JVM运行的原理,Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行,而JVM是java的核心和基础,在java编译器和os平台之间的虚拟处理器. 1.Java语言运行的过程 Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’(.class文件,也就是0,1二进制程序),然后在OS之上的Java解释器中解释执行. 注:JVM(jav

java获取它运行所在目录信息

对于Java程序,无论是未打包的还是打包的JAR或WAR文件,有时候都需要获取它运行所在目录信息,如何做到这一点呢?  在Java处理的文件系统中,目录的表示方式有两种:  (1)绝对目录,它以"/"为起始字符,代表从根目录下开始寻找给出的目录,如/c:/java  (2)相对路径,它以不带"/"的目录名表示,表示以当前Java程序正在运行的目录作为起始目录来寻找给出的目录.如java/classes.在相对路径中,有一些特定的字符,可以代表特的的目录,比如,&qu

通过Java获取图片的信息

1.   图片:96 * 96 2. 程序如下: package com.bnc.bufferedImage; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; public

Java获取properties配置文件信息

调用方法:String url = PropertiesUtil.getProperty("url"); public class PropertiesUtil { public static Properties property; static { property = new Properties(); InputStream in = PropertiesUtil.class.getResourceAsStream("/init.properties");

Java获取异常堆栈信息

public String getExceptionStack(Exception e){ StackTraceElement[] stackTraceElements = e.getStackTrace(); String prefix = "Exception in thread "+"\""+Thread.currentThread().getName()+"\" "; String result = prefix+e.

JAVA代码中获取JVM信息

一.JAVA中获取JVM的信息 原理,利用JavaSDK自带的ManagementFactory类来获取. 二.获取信息 1.获取进程ID @Test public void test1() { RuntimeMXBean mxb = ManagementFactory.getRuntimeMXBean(); System.out.println(mxb.getName()); //[email protected] } 2.获取ManagementFactory.getRuntimeMXBea