JVM监控项
JVM监控原理
JDK的java.lang.management包提供了管理接口,用于监控管理JVM及操作系统。如上图所示,JVM有众多监控项,根据JMX规范,每个监控项用MXBean表示,应用程序通过访问MXBean接口获取监控数据。访问方式分为两种:
1. 直接访问
通过静态工厂方法直接获取对应的MXBean,然后获取监控数据;
2. 间接访问
通过MBeanServer间接访问MXBean:其它应用程序需要(本地或远程)通过特定的MBeanServerConnection连接到MBeanServer;
代码示例
通过直接访问的方式获取监控的代码示例如下所示:
JVM概述
/**
* 类描述:JVM信息工具类
*
* @author ruipeng.lrp
* @since 2017/10/23
**/
public class JVMInfoUtils {
static private RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
static private ClassLoadingMXBean classLoad = ManagementFactory.getClassLoadingMXBean();
static private CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
static private Properties properties = System.getProperties();
// JVM规范名称
static public String getJVMSpecName() {
return runtime.getSpecName();
}
// JVM规范运营商
static public String getJVMSpecVendor() {
return runtime.getSpecVendor();
}
// JVM规范版本
static public String getJVMSpecVersion() {
return runtime.getSpecVersion();
}
// JVM名称
static public String getJVMName() {
return runtime.getVmName();
}
// JVM运营商
static public String getJVMVendor() {
return runtime.getVmVendor();
}
// JVM实现版本
static public String getJVMVersion() {
return runtime.getVmVersion();
}
// JVM启动时间
static public long getJVMStartTimeMs() {
return runtime.getStartTime();
}
// JVM运行时间
static public long getJVMUpTimeMs() {
return runtime.getUptime();
}
// JVM当前加载类总量
static public long getJVMLoadedClassCount() {
return classLoad.getLoadedClassCount();
}
// JVM已卸载类总量
static public long getJVMUnLoadedClassCount() {
return classLoad.getUnloadedClassCount();
}
// JVM从启动到现在加载类总量
static public long getJVMTotalLoadedClassCount() {
return classLoad.getTotalLoadedClassCount();
}
// JIT编译器名称
static public String getJITName() {
return compilation.getName();
}
// JIT总编译时间
static public long getJITTimeMs() {
if (compilation.isCompilationTimeMonitoringSupported()) {
return compilation.getTotalCompilationTime();
}
return -1;
}
// 获取指定key的属性值
static public String getSystemProperty(String key) {
return properties.getProperty(key);
}
static public Properties getSystemProperty() {
return properties;
}
}
JVM内存
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.util.List;
/**
* 类描述:JVM内存信息工具类
*
* @author ruipeng.lrp
* @since 2017/10/23
**/
public class JVMMemoryUtils {
static private MemoryMXBean memoryMXBean;
static private MemoryPoolMXBean permGenMxBean;
static private MemoryPoolMXBean oldGenMxBean;
static private MemoryPoolMXBean edenSpaceMxBean;
static private MemoryPoolMXBean pSSurvivorSpaceMxBean;
static {
memoryMXBean = ManagementFactory.getMemoryMXBean();
List<MemoryPoolMXBean> list = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean item : list) {
if ("CMS Perm Gen".equals(item.getName()) //
|| "Perm Gen".equals(item.getName()) //
|| "PS Perm Gen".equals(item.getName()) //
|| "G1 Perm Gen".equals(item.getName()) //
) {
permGenMxBean = item;
} else if ("CMS Old Gen".equals(item.getName()) //
|| "Tenured Gen".equals(item.getName()) //
|| "PS Old Gen".equals(item.getName()) //
|| "G1 Old Gen".equals(item.getName()) //
) {
oldGenMxBean = item;
} else if ("Par Eden Space".equals(item.getName()) //
|| "Eden Space".equals(item.getName()) //
|| "PS Eden Space".equals(item.getName()) //
|| "G1 Eden".equals(item.getName()) //
) {
edenSpaceMxBean = item;
} else if ("Par Survivor Space".equals(item.getName()) //
|| "Survivor Space".equals(item.getName()) //
|| "PS Survivor Space".equals(item.getName()) //
|| "G1 Survivor".equals(item.getName()) //
) {
pSSurvivorSpaceMxBean = item;
}
}
}// static
// 堆内存-已使用
static public long getHeapMemoryUsed() {
return memoryMXBean.getHeapMemoryUsage().getUsed();
}
// 堆内存-最大值
static public long getHeapMemoryMax() {
return memoryMXBean.getHeapMemoryUsage().getMax();
}
// 堆外内存-已使用
static public long getNonHeapMemoryUsed() {
return memoryMXBean.getNonHeapMemoryUsage().getUsed();
}
// 堆外内存-最大值
static public long getNonHeapMemoryMax() {
return memoryMXBean.getNonHeapMemoryUsage().getMax();
}
// 持久代-已使用
static public long getPermGenUsed() {
return null == permGenMxBean ? 0 : permGenMxBean.getUsage().getUsed();
}
// 持久代-最大值
static public long getPermGenMax() {
return null == permGenMxBean ? 0 : permGenMxBean.getUsage().getMax();
}
// 老年代-已使用
static public long getOldGenUsed() {
return null == oldGenMxBean ? 0 : oldGenMxBean.getUsage().getUsed();
}
// 老年代-最大值
static public long getOldGenMax() {
return null == oldGenMxBean ? 0 : oldGenMxBean.getUsage().getMax();
}
// Eden-已使用
static public long getEdenGenUsed() {
return null == edenSpaceMxBean ? 0 : edenSpaceMxBean.getUsage().getUsed();
}
// Eden-最大值
static public long getEdenGenMax() {
return null == edenSpaceMxBean ? 0 : edenSpaceMxBean.getUsage().getMax();
}
// Survivor-已使用
static public long getSurvivorUsed() {
return null == pSSurvivorSpaceMxBean ? 0 : pSSurvivorSpaceMxBean.getUsage().getUsed();
}
// Survivor-最大值
static public long getSurvivorMax() {
return null == pSSurvivorSpaceMxBean ? 0 : pSSurvivorSpaceMxBean.getUsage().getMax();
}
public static void main(String[] args) {
System.out.println(JVMMemoryUtils.getHeapMemoryMax());
System.out.println(JVMMemoryUtils.getHeapMemoryUsed());
System.out.println(JVMMemoryUtils.getNonHeapMemoryMax());
System.out.println(JVMMemoryUtils.getNonHeapMemoryUsed());
System.out.println(JVMMemoryUtils.getOldGenMax());
System.out.println(JVMMemoryUtils.getOldGenUsed());
System.out.println(JVMMemoryUtils.getPermGenMax());
System.out.println(JVMMemoryUtils.getPermGenUsed());
System.out.println(JVMMemoryUtils.getEdenGenMax());
System.out.println(JVMMemoryUtils.getEdenGenUsed());
System.out.println(JVMMemoryUtils.getSurvivorMax());
System.out.println(JVMMemoryUtils.getSurvivorUsed());
}
}
JVM GC
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.ListIterator;
/**
* 类描述:JVM GC信息工具类
*
* @author ruipeng.lrp
* @since 2017/10/23
**/
public class JVMGCUtils {
static private GarbageCollectorMXBean fullGC;
static private GarbageCollectorMXBean youngGC;
static{
for (ListIterator<GarbageCollectorMXBean> iter = ManagementFactory.getGarbageCollectorMXBeans().listIterator(); iter.hasNext();) {
GarbageCollectorMXBean item = iter.next();
if ("ConcurrentMarkSweep".equals(item.getName()) //
|| "MarkSweepCompact".equals(item.getName()) //
|| "PS MarkSweep".equals(item.getName()) //
|| "G1 Old Generation".equals(item.getName()) //
|| "Garbage collection optimized for short pausetimes Old Collector".equals(item.getName()) //
|| "Garbage collection optimized for throughput Old Collector".equals(item.getName()) //
|| "Garbage collection optimized for deterministic pausetimes Old Collector".equals(item.getName()) //
) {
fullGC = item;
} else if ("ParNew".equals(item.getName()) //
|| "Copy".equals(item.getName()) //
|| "PS Scavenge".equals(item.getName()) //
|| "G1 Young Generation".equals(item.getName()) //
|| "Garbage collection optimized for short pausetimes Young Collector".equals(item.getName()) //
|| "Garbage collection optimized for throughput Young Collector".equals(item.getName()) //
|| "Garbage collection optimized for deterministic pausetimes Young Collector".equals(item.getName()) //
) {
youngGC = item;
}
}
}//static
//YGC总次数
static public long getYoungGCCollectionCount() {
return youngGC == null ? 0 : youngGC.getCollectionCount();
}
//YGC总时间
static public long getYoungGCCollectionTime() {
return youngGC == null ? 0 : youngGC.getCollectionTime();
}
//FGC总次数
public long getFullGCCollectionCount() {
return fullGC == null ? 0 : fullGC.getCollectionCount();
}
//FGC总次数
public long getFullGCCollectionTime() {
return fullGC == null ? 0 : fullGC.getCollectionTime();
}
}
JVM 线程
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
/**
* 类描述:JVM 线程信息工具类
*
* @author ruipeng.lrp
* @since 2017/10/23
**/
public class JVMThreadUtils {
static private ThreadMXBean threadMXBean;
static {
threadMXBean = ManagementFactory.getThreadMXBean();
}
//Daemon线程总量
static public int getDaemonThreadCount() {
return threadMXBean.getDaemonThreadCount();
}
//当前线程总量
static public int getThreadCount() {
return threadMXBean.getThreadCount();
}
//死锁线程总量
static public int getDeadLockedThreadCount() {
try {
long[] deadLockedThreadIds = threadMXBean.findDeadlockedThreads();
if (deadLockedThreadIds == null) {
return 0;
}
return deadLockedThreadIds.length;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}

5326

被折叠的 条评论
为什么被折叠?



