JAVA代码中获取JVM信息
一、JAVA中获取JVM的信息
原理,利用JavaSDK自带的ManagementFactory类来获取。
二、获取信息
1、获取进程ID
@Test public void test1() { RuntimeMXBean mxb = ManagementFactory.getRuntimeMXBean(); System.out.println(mxb.getName()); //2668@Sindrol-PC }
2、获取ManagementFactory.getRuntimeMXBean()的其它信息
RuntimeMXBean mxb = ManagementFactory.getRuntimeMXBean();
mxb.getSystemProperties() = {HashMap@866} size = 56 mxb.getInputArguments() = {Collections$UnmodifiableRandomAccessList@1039} size = 5 mxb.getBootClassPath() = "C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\classes;D:\Applicaton\DevTools\JetBrains\IntelliJIDEA\lib\rt\debugger-agent-storage.jar" mxb.getLibraryPath() = "C:\Program Files\Java\jdk1.8.0_161\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64;D:\Applicaton\DevTools\Python\3.5\Scripts\;D:\Applicaton\DevTools\Python\3.5\;D:\Applicaton\DevTools\cudnn-8.0-windows10-x64-v5.1\cuda;D:\Applicaton\DevTools\cudnn-8.0-windows10-x64-v5.1\cuda\bin;D:\Applicaton\DevTools\cudnn-8.0-windows10-x64-v5.1\cuda\lib\x64;D:\Applicaton\DevTools\cudnn-8.0-windows10-x64-v5.1\cuda\lib;C:\Program Files (x86)\Common Files\NetSarang;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft SDKs\Azure\CL" mxb.getClassPath() = "D:\Applicaton\DevTools\JetBrains\IntelliJIDEA\lib\idea_rt.jar;D:\Applicaton\DevTools\JetBrains\IntelliJIDEA\plugins\junit\lib\junit-rt.jar;D:\Applicaton\DevTools\JetBrains\IntelliJIDEA\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Pr" mxb.getManagementSpecVersion() = "1.2" mxb.getSpecName() = "Java Virtual Machine Specification" mxb.getSpecVendor() = "Oracle Corporation" mxb.getStartTime() = 1527587462275 mxb.getUptime() = 95372 mxb.getVmVendor() = "Oracle Corporation" mxb.getObjectName() = {ObjectName@1052} "java.lang:type=Runtime" mxb.getName() = "2668@Sindrol-NTB"
3、获取操作系统信息
@Test public void test2() { OperatingSystemMXBean osb = ManagementFactory.getOperatingSystemMXBean(); System.out.println(osb.getName()); //Windows 10 System.out.println(osb.getArch()); //amd 64 System.out.println(osb.getAvailableProcessors()); //4 System.out.println(osb.getVersion()); //10.0 System.out.println(osb.getSystemLoadAverage()); //-1.0 }
4、获取JVM的内存
@Test public void test4() { MemoryMXBean mxb = ManagementFactory.getMemoryMXBean(); //Heap System.out.println("Max:" + mxb.getHeapMemoryUsage().getMax() / 1024 / 1024 + "MB"); //Max:1776MB System.out.println("Init:" + mxb.getHeapMemoryUsage().getInit() / 1024 / 1024 + "MB"); //Init:126MB System.out.println("Committed:" + mxb.getHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB"); //Committed:121MB System.out.println("Used:" + mxb.getHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB"); //Used:7MB System.out.println(mxb.getHeapMemoryUsage().toString()); //init = 132120576(129024K) used = 8076528(7887K) committed = 126877696(123904K) max = 1862270976(1818624K) //Non heap System.out.println("Max:" + mxb.getNonHeapMemoryUsage().getMax() / 1024 / 1024 + "MB"); //Max:0MB System.out.println("Init:" + mxb.getNonHeapMemoryUsage().getInit() / 1024 / 1024 + "MB"); //Init:2MB System.out.println("Committed:" + mxb.getNonHeapMemoryUsage().getCommitted() / 1024 / 1024 + "MB"); //Committed:8MB System.out.println("Used:" + mxb.getNonHeapMemoryUsage().getUsed() / 1024 / 1024 + "MB"); //Used:7MB System.out.println(mxb.getNonHeapMemoryUsage().toString()); //init = 2555904(2496K) used = 7802056(7619K) committed = 9109504(8896K) max = -1(-1K) }
5、获取JVM的内存池情况
@Test public void test5() { List<MemoryPoolMXBean> mxb = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean memoryPoolMXBean : mxb) { System.out.println("Name:" + memoryPoolMXBean.getName()); System.out.println("Usage:" + memoryPoolMXBean.getUsage()); System.out.println("Manager:" + String.join(",", memoryPoolMXBean.getMemoryManagerNames())); System.out.println("Type:" + memoryPoolMXBean.getType()); System.out.println("--------------------------"); } }
结果:
Name:Code Cache Usage:init = 2555904(2496K) used = 1742656(1701K) committed = 2555904(2496K) max = 251658240(245760K) Manager:CodeCacheManager Type:Non-heap memory -------------------------- Name:Metaspace Usage:init = 0(0K) used = 5436960(5309K) committed = 5767168(5632K) max = -1(-1K) Manager:Metaspace Manager Type:Non-heap memory -------------------------- Name:Compressed Class Space Usage:init = 0(0K) used = 632000(617K) committed = 786432(768K) max = 1073741824(1048576K) Manager:Metaspace Manager Type:Non-heap memory -------------------------- Name:PS Eden Space Usage:init = 33554432(32768K) used = 8076056(7886K) committed = 33554432(32768K) max = 687865856(671744K) Manager:PS MarkSweep,PS Scavenge Type:Heap memory -------------------------- Name:PS Survivor Space Usage:init = 5242880(5120K) used = 0(0K) committed = 5242880(5120K) max = 5242880(5120K) Manager:PS MarkSweep,PS Scavenge Type:Heap memory -------------------------- Name:PS Old Gen Usage:init = 88080384(86016K) used = 0(0K) committed = 88080384(86016K) max = 1396703232(1363968K) Manager:PS MarkSweep Type:Heap memory --------------------------
6、获取程序线程信息
@Test public void test6() { ThreadMXBean tmx = ManagementFactory.getThreadMXBean(); System.out.println("thread count:" + tmx.getThreadCount()); for (long id : tmx.getAllThreadIds()) { ThreadInfo ti = tmx.getThreadInfo(id); System.out.println(ti.toString().trim()); System.out.println("cpu time:" + tmx.getThreadCpuTime(id)); System.out.println("user time:" + tmx.getThreadUserTime(id)); System.out.println("-----------------"); } System.out.println("findDeadlockedThreads:"); if (tmx.findDeadlockedThreads() != null) for (long id : tmx.findDeadlockedThreads()) { ThreadInfo ti = tmx.getThreadInfo(id); System.out.println(ti.toString().trim()); } }
结果:
thread count:6 "Monitor Ctrl-Break" Id=6 RUNNABLE (in native) cpu time:0 user time:0 ----------------- "Attach Listener" Id=5 RUNNABLE cpu time:0 user time:0 ----------------- "Signal Dispatcher" Id=4 RUNNABLE cpu time:0 user time:0 ----------------- "Finalizer" Id=3 WAITING on java.lang.ref.ReferenceQueue$Lock@22927a81 cpu time:0 user time:0 ----------------- "Reference Handler" Id=2 WAITING on java.lang.ref.Reference$Lock@78e03bb5 cpu time:0 user time:0 ----------------- "main" Id=1 RUNNABLE cpu time:203125000 user time:171875000 ----------------- findDeadlockedThreads:
宋兴柱:转载内容,请标明出处,谢谢!源文来自 宝贝云知识分享:https://www.dearcloud.cn