在java代碼中獲取JVM參數(shù)的方法
實(shí)例如下:
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memorymbean.getHeapMemoryUsage();
System.out.println("INIT HEAP: " + usage.getInit());
System.out.println("MAX HEAP: " + usage.getMax());
System.out.println("USE HEAP: " + usage.getUsed());
System.out.println("\nFull Information:");
System.out.println("Heap Memory Usage: "
+ memorymbean.getHeapMemoryUsage());
System.out.println("Non-Heap Memory Usage: "
+ memorymbean.getNonHeapMemoryUsage());
List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
System.out.println("===================java options=============== ");
System.out.println(inputArguments);
System.out.println("=======================通過(guò)java來(lái)獲取相關(guān)系統(tǒng)狀態(tài)============================ ");
int i = (int)Runtime.getRuntime().totalMemory()/1024;//Java 虛擬機(jī)中的內(nèi)存總量,以字節(jié)為單位
System.out.println("總的內(nèi)存量 i is "+i);
int j = (int)Runtime.getRuntime().freeMemory()/1024;//Java 虛擬機(jī)中的空閑內(nèi)存量
System.out.println("空閑內(nèi)存量 j is "+j);
System.out.println("最大內(nèi)存量 is "+Runtime.getRuntime().maxMemory()/1024);
System.out.println("=======================OperatingSystemMXBean============================ ");
OperatingSystemMXBean osm = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// System.out.println(osm.getFreeSwapSpaceSize()/1024);
// System.out.println(osm.getFreePhysicalMemorySize()/1024);
// System.out.println(osm.getTotalPhysicalMemorySize()/1024);
//獲取操作系統(tǒng)相關(guān)信息
System.out.println("osm.getArch() "+osm.getArch());
System.out.println("osm.getAvailableProcessors() "+osm.getAvailableProcessors());
//System.out.println("osm.getCommittedVirtualMemorySize() "+osm.getCommittedVirtualMemorySize());
System.out.println("osm.getName() "+osm.getName());
//System.out.println("osm.getProcessCpuTime() "+osm.getProcessCpuTime());
System.out.println("osm.getVersion() "+osm.getVersion());
//獲取整個(gè)虛擬機(jī)內(nèi)存使用情況
System.out.println("=======================MemoryMXBean============================ ");
MemoryMXBean mm=(MemoryMXBean)ManagementFactory.getMemoryMXBean();
System.out.println("getHeapMemoryUsage "+mm.getHeapMemoryUsage());
System.out.println("getNonHeapMemoryUsage "+mm.getNonHeapMemoryUsage());
//獲取各個(gè)線程的各種狀態(tài),CPU 占用情況,以及整個(gè)系統(tǒng)中的線程狀況
System.out.println("=======================ThreadMXBean============================ ");
ThreadMXBean tm=(ThreadMXBean)ManagementFactory.getThreadMXBean();
System.out.println("getThreadCount "+tm.getThreadCount());
System.out.println("getPeakThreadCount "+tm.getPeakThreadCount());
System.out.println("getCurrentThreadCpuTime "+tm.getCurrentThreadCpuTime());
System.out.println("getDaemonThreadCount "+tm.getDaemonThreadCount());
System.out.println("getCurrentThreadUserTime "+tm.getCurrentThreadUserTime());
//當(dāng)前編譯器情況
System.out.println("=======================CompilationMXBean============================ ");
CompilationMXBean gm=(CompilationMXBean)ManagementFactory.getCompilationMXBean();
System.out.println("getName "+gm.getName());
System.out.println("getTotalCompilationTime "+gm.getTotalCompilationTime());
//獲取多個(gè)內(nèi)存池的使用情況
System.out.println("=======================MemoryPoolMXBean============================ ");
List<MemoryPoolMXBean> mpmList=ManagementFactory.getMemoryPoolMXBeans();
for(MemoryPoolMXBean mpm:mpmList){
System.out.println("getUsage "+mpm.getUsage());
System.out.println("getMemoryManagerNames "+mpm.getMemoryManagerNames().toString());
}
//獲取GC的次數(shù)以及花費(fèi)時(shí)間之類的信息
System.out.println("=======================MemoryPoolMXBean============================ ");
List<GarbageCollectorMXBean> gcmList=ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean gcm:gcmList){
System.out.println("getName "+gcm.getName());
System.out.println("getMemoryPoolNames "+gcm.getMemoryPoolNames());
}
//獲取運(yùn)行時(shí)信息
System.out.println("=======================RuntimeMXBean============================ ");
RuntimeMXBean rmb=(RuntimeMXBean)ManagementFactory.getRuntimeMXBean();
System.out.println("getClassPath "+rmb.getClassPath());
System.out.println("getLibraryPath "+rmb.getLibraryPath());
System.out.println("getVmVersion "+rmb.getVmVersion());
以上這篇在java代碼中獲取JVM參數(shù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot集成spring cache緩存示例代碼
本篇文章主要介紹了springboot集成spring cache示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
java中對(duì)字符串每個(gè)字符統(tǒng)計(jì)的方法
java中對(duì)字符串每個(gè)字符統(tǒng)計(jì)的方法,需要的朋友可以參考一下2013-03-03
使用@slf4j找不到符號(hào)log的問(wèn)題及解決
這篇文章主要介紹了使用@slf4j找不到符號(hào)log的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
springBoot接入阿里云oss的實(shí)現(xiàn)步驟
這篇文章主要介紹了springBoot接入阿里云oss的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringSecurity-2.7中跨域問(wèn)題解析
這篇文章主要介紹了SpringSecurity-2.7中跨域問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
idea新建聚合項(xiàng)目并附上標(biāo)簽的詳細(xì)過(guò)程
這篇文章主要介紹了idea新建聚合項(xiàng)目并附上標(biāo)簽的詳細(xì)過(guò)程,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
java.lang.IllegalArgumentException:Invalid character&nb
本文介紹了java.lang.IllegalArgumentException: Invalid character found異常的解決,方法包括檢查代碼中的方法名,使用合適的HTTP請(qǐng)求方法常量,使用第三方HTTP庫(kù),檢查請(qǐng)求URL以及使用調(diào)試和日志工具,通過(guò)這些方法,我們可以解決異常并確保網(wǎng)絡(luò)應(yīng)用程序的正常運(yùn)行2023-10-10
基于SpringBoot實(shí)現(xiàn)自定義插件的流程詳解
在SpringBoot中,插件是一種擴(kuò)展機(jī)制,它可以幫助我們?cè)趹?yīng)用程序中快速地添加一些額外的功能,在本文中,我們將介紹如何使用 SpringBoot實(shí)現(xiàn)自定義插件,需要的朋友可以參考下2023-06-06

