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