1.代码调用内存情况 Runtime runtime = Runtime.getRuntime();
1)JVM可用最大内存:long max = runtime.maxMemory() (byte)
2)JVM占用总内存:long total = runtime.totalMemory() (byte)
3)JVM空闲内存 : long free = runtime.freeMemory() (byte)
4)JVM可用处理器: int availableProcessors = runtime.availableProcessors();
2.线程组使用情况:
ThreadGroup group = Thread.currentThread().getThreadGroup();
ThreadGroup topGroup = group;// 遍历线程组树,获取根线程组
while (group != null) { topGroup = group; group = group.getParent();}
1)当前的活动线程总数 : topGroup.activeCount();
2)线程组总数 : topGroup.activeGroupCount();
3.ManagemntFactory : GC、内存、编译等管理情况
1)获取GC信息
long gcCounts = 0;
long gcTimes = 0;
for (final GarbageCollectorMXBean gcCollector : ManagementFactory.getGarbageCollectorMXBeans()) {
gcCounts += gcCollector.getCollectionCount();
gcTimes += gcCollector.getCollectionTime();
}
2).获取Memory信息
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
long getFinallzationCount = memoryMxBean.getObjectPendingFinalizationCount();
String heapMemoryUsage = memoryMxBean.getHeapMemoryUsage().toString();
String nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage().toString();