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();