简体   繁体   English

如何衡量Java中的峰值堆内存使用情况?

[英]How to measure peak heap memory usage in Java?

How to measure peak heap memory usage in Java? 如何衡量Java中的峰值堆内存使用情况? MemoryPoolMXBean keeps track of the peak usage per memory pool, but not the entire heap. MemoryPoolMXBean跟踪每个内存池的峰值使用情况,但不记录整个堆。 And the peak heap usage is not simply the sum of different heap memory pools. 峰值堆使用量不仅仅是不同堆内存池的总和。

Did you think about using totalMemory() function from Runtime class - docs ? 您是否考虑过使用Runtime类的totalMemory()函数 - docs Also there are some free tools like VisualVM or JStat , example: 还有一些像VisualVMJStat这样的免费工具,例如:

jstat -gc <pid> <time> <amount>

Hope that helps. 希望有所帮助。

If you need the peak heap size, you can combine the peak used size of all the memory pools which are of type HEAP . 如果需要峰堆大小,可以组合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 (我还没试过)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM