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:
JAVA代码中获取JVM信息的更多相关文章
- 在java代码中获取JVM参数(转)
近日关注性能调优,关注JMX,发现java.lang.management.*之强大.同时查阅了资料,整合一版关于JVM参数获取的note,仅供参考: MemoryMXBean memorymbean ...
- java代码中获取进程process id(转)
另一方面,线程ID=进程ID+内部线程对象ID并不成立, 参考: blog.csdn.net/heyetina/article/details/6633901 如何在java代码中获取进 ...
- Java代码中获取Json的key值
测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...
- Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...
- java代码中获取classpath路径
Javaweb工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: String path = Test.class. ...
- Android开发之java代码中获取当前系统的时间工具类
/** * 获取当前时间 * * @return */ public String getTime() { Date date = new Date();// 创建一个时间对象,获取到当前的时间 Si ...
- 【Java/JDBC】利用ResultSetMetaData从数据库的某表中获取字段信息并存到csv文件
代码下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-2.rar 这篇还不够完善,请看更完善的续篇 ...
- java如何从一段html代码中获取图片的src路径
java如何从一段html代码中获取图片的src路径 package com.cellstrain.icell.Test; import java.util.ArrayList;import java ...
- C#中获取文件信息的代码
如下的内容内容是关于C#中获取文件信息的内容,应该对大伙有一些好处. FileInfo fi = new FileInfo(@"C:file.txt"); if(fi.Exists ...
随机推荐
- Math 对象
Math对象提供了,我们一般进行数学运算的所有函数. Math.random() 随机0~1之间的随机数 [0, 1) Math.max() 求传入参数的最大数 Math.min() 求传入参数的最小 ...
- 微信公众号开发JS-SDK(1.2)
概述 微信js-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微 ...
- python 全栈开发,Day45(html介绍和head标签,body标签中相关标签)
一.html介绍 1.web标准 web准备介绍: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web准备规范的分类:结构标准.表现标准.行为标准. 结构 ...
- ruby学习-字符串
字符串 1.创建字符1:new用来创建新字符,empty?检验字符是否为空 title = String.new #=> "" title.empty? #=>true ...
- RDLC 主从报表筛选
今天继续学习RDLC报表的“参数传递”及“主从报表” 一.先创建DataSet,如下图: 二.创建一个报表rptDEPT.rdlc,显示部门T_DPET的数据 三.嵌入Default.aspx中,写在 ...
- POJ 3040 Allowance【贪心】
POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...
- c++ primer 笔记 (四)
现代C++程序应尽量使用vector和迭代器类型,而避免使用低级的数组和指针. 设计良好的程序只有在强调速度时才在类实现的内部实现数组和指针. 更多地使用vector来取代数组,数组被严格限制于程 ...
- jenkins X实践系列(2) —— 基于jx的DevOps实践
jx是云原生CICD,devops的一个最佳实践之一,目前在快速的发展成熟中.最近调研了JX,这里为第2篇,使用已经安装好的jx来实践CICD,旨在让大家了解基于jx的DevOps是如何运转的,感兴趣 ...
- zstu-4243 牛吃草
贴一发两圆相交面积模板 #include<bits/stdc++.h> #define pi acos(-1.0) using namespace std; ; double _abs(d ...
- 洛谷 p1164 小A点菜 【dp(好题)】 || 【DFS】 【恰好完全装满】
题目链接:https://www.luogu.org/problemnew/show/P1164 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. u ...