java get peak of used heap memory

209 views Asked by At

Is there a way to precisely get maximum used heap memory?

I was using below piece of code

String memoryUsage = new String();
    List pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean pool : pools) {
        MemoryUsage peak = pool.getPeakUsage();
        memoryUsage += String.format("Peak %s memory used: %,d%n", pool.getName(),peak.getUsed());
        memoryUsage += String.format("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
    }
    // we print the result in the console
    System.out.println(memoryUsage);

But it does not show the same results like in profiler (results are quite different). Is this correct way to get maximum used heap memory in Java application?

0

There are 0 answers