侧边栏壁纸
博主头像
Monkey部落博主等级

Monkey部落,分享技术、经验、遇到的问题及解决方法,欢迎大家互相讨论分享。

  • 累计撰写 59 篇文章
  • 累计创建 36 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

JAVA获取jvm和操作系统相关信息

Monkey部落
2022-10-13 / 0 评论 / 1 点赞 / 252 阅读 / 925 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-10-18,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

JAVA获取jvm和操作系统相关信息

背景

今日搬砖🧱时需要获取系统运行时间、版本号等相关信息,使用Java自带的类进行获取系统运行的相关信息,在这整理记录分享一下,感兴趣的小伙伴可以自己尝试尝试。

Jvm

首先获取jvm相关信息,包含jvm的名称、版本号、启动时间、运行时间、环境变量、进程id等等

public class Test {
    public static void main(String[] args) {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        // jvmName
        System.out.printf("jvmName: %s %n", runtimeMXBean.getVmName());
        // jvmVersion
        System.out.printf("jvmVersion: %s %n", runtimeMXBean.getVmVersion());
        // jvmVendor
        System.out.printf("jvmVendor: %s %n", runtimeMXBean.getVmVendor());
        // startTime 使用hutool中DateUtil进行转换
        long startTime = runtimeMXBean.getStartTime();
        System.out.printf("startTime: %s %n", DateUtil.date(startTime).toString());
        // updateTime
        long uptime = runtimeMXBean.getUptime();
        System.out.printf("updateTime: %s %n", DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND));
        // classPath
        System.out.printf("classPath: %s %n", runtimeMXBean.getClassPath());
        // systemProperties
        System.out.printf("jvmName: %s %n", runtimeMXBean.getSystemProperties());
        // bootClassPath
        System.out.printf("bootClassPath: %s %n", runtimeMXBean.getBootClassPath());
        // processId
        System.out.printf("processId: %s %n", runtimeMXBean.getName().split("@")[0]);
    }
}

还可以获取JVM内存相关信息,例如堆内存。

public class Test {
    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        // heapMemoryUsed
        System.out.printf("heapMemoryUsed: %d MB %n", heapMemoryUsage.getUsed()/1024/1024);
        // heapMemoryMax
        System.out.printf("heapMemoryMax: %d MB %n", heapMemoryUsage.getMax()/1024/1024);
        // heapMemoryCommitted
        System.out.printf("heapMemoryCommitted: %d MB %n", heapMemoryUsage.getCommitted()/1024/1024);
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        // nonHeapMemoryUsed
        System.out.printf("nonHeapMemoryUsed: %d MB %n", nonHeapMemoryUsage.getUsed()/1024/1024);
        // nonHeapMemoryMax
        System.out.printf("nonHeapMemoryMax: %d MB %n", nonHeapMemoryUsage.getMax()/1024/1024);
        // nonHeapMemoryCommitted
        System.out.printf("nonHeapMemoryCommitted: %d MB %n", nonHeapMemoryUsage.getCommitted()/1024/1024);
    }
}

获取JDK相关信息。包含jdk的版本、安装路径、当前运行jar包路径、运行jar文件名等。

public class Test {
    public static void main(String[] args) {
        // jdkVersion
        System.out.printf("jdkVersion: %s %n", System.getProperty("java.version"));
        // java_home
        System.out.printf("java_home: %s %n", System.getProperty("java.home"));
        // jar包路径
        String path = System.getProperty("java.class.path");
        int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
        int lastIndex = path.lastIndexOf(File.separator) + 1;
        String jarPath = path.substring(firstIndex, lastIndex);
        System.out.printf("jarPath: %s %n", jarPath);
        // 当前运行jar文件名
        String jarName = path.substring(lastIndex);
        System.out.printf("jarName: %s %n", jarName);
    }
}

获取java虚拟机线程信息,包含线程的阻塞时间、次数、线程的堆栈信息等等。

public class Test {
    public static void main(String[] args) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        // ThreadCount
        System.out.printf("ThreadCount: %d %n", threadMXBean.getThreadCount());
        // AllThreadIds
        System.out.printf("AllThreadIds: %s %n", threadMXBean.getAllThreadIds());
        // TotalStartedThread
        System.out.printf("TotalStartedThread: %d %n", threadMXBean.getTotalStartedThreadCount());
        // DaemonThread
        System.out.printf("DaemonThread: %d %n", threadMXBean.getDaemonThreadCount());
        // PeakThread
        System.out.printf("PeakThread: %d %n", threadMXBean.getPeakThreadCount());
        // ThreadInfo
        System.out.printf("ThreadInfo: %s %n", threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()));
    }
}

获取java虚拟机类加载相关信息。

public class Test {
    public static void main(String[] args) {
        ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
        // LoadedClassCount
        System.out.printf("LoadedClassCount: %d %n", classLoadingMXBean.getLoadedClassCount());
        // TotalLoadedClassCount
        System.out.printf("TotalLoadedClassCount: %d %n", classLoadingMXBean.getTotalLoadedClassCount());
        // UnloadedClassCount
        System.out.printf("UnloadedClassCount: %d %n", classLoadingMXBean.getUnloadedClassCount());
    }
}

操作系统

获取操作系统以及主机硬件信息,包含系统名称、版本、物理内存、可用内存等等。

public class Test {
    public static void main(String[] args) {
        // 系统版本
        OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean();
        // osName
        System.out.printf("osName: %s %n", operatingSystemMXBean.getName());
        // Arch
        System.out.printf("Arch: %s %n", operatingSystemMXBean.getArch());
        // Version
        System.out.printf("Version: %s %n", operatingSystemMXBean.getVersion());
        // 物理内存
        long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize()/1024/1024/1024;
        // totalPhysicalMemorySize
        System.out.printf("totalPhysicalMemorySize: %d GB %n", totalPhysicalMemorySize);
    }
}

关注微信公众号「平哥技术站」, 每日更新,在手机上阅读所有教程,随时随地都能学习。

觉得写的还不错的小伙伴,请作者喝杯咖啡☕ ,支持一下。😊

如有侵权请立即与我们联系,我们将及时处理,联系邮箱:865934097@qq.com。

1

评论区