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?