How to measure peak heap memory usage in Java?

How to measure peak heap memory usage in Java? MemoryPoolMXBean keeps track of maximum usage for each memory pool, but not the entire heap. And peak heap usage is not just the sum of the different heap memory pools.

+5
source share
2 answers

Have you considered using the totalMemory() function from the Runtime class - docs ? There are also some free tools, such as VisualVM or JStat , an example:

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

Hope this helps.

+4
source

If you need a maximum heap size, you can combine the peak size of all memory pools that are of HEAP type.

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)); 
0
source

Source: https://habr.com/ru/post/1239952/


All Articles