JAVA如何獲取jvm和操作系統(tǒng)相關(guān)信息
什么是JVM
JVM是JavaVirtualMachine(Java虛擬機(jī))的縮寫,JVM是一種用于計(jì)算設(shè)備的規(guī)范,它是一個(gè)虛構(gòu)出來(lái)的計(jì)算機(jī),是通過在實(shí)際的計(jì)算機(jī)上仿真模擬各種計(jì)算機(jī)功能來(lái)實(shí)現(xiàn)的。 主流虛擬機(jī)
背景
今日搬磚??時(shí)需要獲取系統(tǒng)運(yùn)行時(shí)間、版本號(hào)等相關(guān)信息,使用Java自帶的類進(jìn)行獲取系統(tǒng)運(yùn)行的相關(guān)信息,在這整理記錄分享一下,感興趣的小伙伴可以自己嘗試嘗試。
Jvm
首先獲取jvm相關(guān)信息,包含jvm的名稱、版本號(hào)、啟動(dòng)時(shí)間、運(yùn)行時(shí)間、環(huán)境變量、進(jìn)程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進(jìn)行轉(zhuǎn)換 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內(nèi)存相關(guān)信息,例如堆內(nèi)存。
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相關(guān)信息。包含jdk的版本、安裝路徑、當(dāng)前運(yùn)行jar包路徑、運(yùn)行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); // 當(dāng)前運(yùn)行jar文件名 String jarName = path.substring(lastIndex); System.out.printf("jarName: %s %n", jarName); } }
獲取java虛擬機(jī)線程信息,包含線程的阻塞時(shí)間、次數(shù)、線程的堆棧信息等等。
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虛擬機(jī)類加載相關(guān)信息。
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()); } }
操作系統(tǒng)
獲取操作系統(tǒng)以及主機(jī)硬件信息,包含系統(tǒng)名稱、版本、物理內(nèi)存、可用內(nèi)存等等。
public class Test { public static void main(String[] args) { // 系統(tǒng)版本 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()); // 物理內(nèi)存 long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize()/1024/1024/1024; // totalPhysicalMemorySize System.out.printf("totalPhysicalMemorySize: %d GB %n", totalPhysicalMemorySize); } }
到此這篇關(guān)于JAVA獲取jvm和操作系統(tǒng)相關(guān)信息的文章就介紹到這了,更多相關(guān)JAVA獲取jvm信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC Cron定時(shí)器Demo常見問題解決方案
這篇文章主要介紹了SpringMVC Cron定時(shí)器Demo常見問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11SpringCloud Gateway自動(dòng)裝配實(shí)現(xiàn)流程詳解
Spring Cloud Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的、統(tǒng)一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等2022-10-10詳解在Spring中如何使用AspectJ來(lái)實(shí)現(xiàn)AOP
這篇文章主要介紹了詳解在Spring中如何使用AspectJ來(lái)實(shí)現(xiàn)AOP,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-06-06SpringBoot下RabbitMq實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot下RabbitMq實(shí)現(xiàn)定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11SpringBoot多模塊項(xiàng)目框架搭建過程解析
這篇文章主要介紹了SpringBoot多模塊項(xiàng)目框架搭建過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01關(guān)于SpringBoot+Mybatis報(bào)MapperScan.factoryBean()問題
解決SpringBoot+Mybatis中的MapperScan.factoryBean()問題,讓你的項(xiàng)目運(yùn)行更順暢!本指南將帶你一步步解決這個(gè)問題,讓你的開發(fā)過程更加高效,不要錯(cuò)過這個(gè)實(shí)用指南,快來(lái)一探究竟吧!2024-02-02Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例
下面小編就為大家?guī)?lái)一篇Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06