How to measure peak heap memory usage in Java? MemoryPoolMXBean keeps track of the peak usage per memory pool, but not the entire heap. And the peak heap usage is not simply the sum of different heap memory pools.
If you need the peak heap size, you can combine the peak used size of all the memory pools which are of type HEAP .
Here is an example:
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
long total = 0;
for (MemoryPoolMXBean memoryPoolMXBean : pools)
{
if (memoryPoolMXBean.getType() == MemoryType.HEAP)
{
long peakUsed = memoryPoolMXBean.getPeakUsage().getUsed();
System.out.println("Peak used for: " + memoryPoolMXBean.getName() + " is: " + peakUsed);
total = total + peakUsed;
}
}
System.out.println("Total heap peak used: " + Util.format(total));
看看这篇文章https://cruftex.net/2017/03/28/The-6-Memory-Metrics-You-Should-Track-in-Your-Java-Benchmarks.html ,它提到了GarbageCollectionNotificationInfo
并且有实现使用JMH (我还没试过)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.