无意间找到了个好东西,记录下!
1获取JVM输入参数
List<String> list=ManagementFactory.getRuntimeMXBean().getInputArguments();
2.获取当前JVM进程的PID
String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.split("@")[0];
3.获取当前系统的负载
ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()
4.获取内存相关的
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();//堆内存
ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()//堆外内存
5.获取堆栈信息相当于jstack
ManagementFactory.getThreadMXBean().dumpAllThreads(false, false)
ThreadInfo 里有线程的信息
进行垃圾回收监控
Jstat.main(new String[]{"-gcutil", "-h5",pid,"1s"});
如何获取HotSpotDiagnosticMXBean ?
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean hotspotDiagnosticMXBean = newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
//获取young GC 和full GC 次数
List<GarbageCollectorMXBean> list=ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean e:list){
logger.info("name={},count={},time={}",e.getName(),e.getCollectionCount(),e.getCollectionTime());
}