Find the max allocated heap size during the execut

2019-04-09 16:20发布

问题:

I wonder if I can obtain the maximum size of the allocated heap using VM command line.

From now I'm able to get it using Netbeans Profiler but I prefer getting result without have to launch an extra application.

I'm also using this method :

public void printMemUsage(){
        double currentMemory = ( (double)((double)(Runtime.getRuntime().totalMemory()/1024.0)))- ((double)((double)(Runtime.getRuntime().freeMemory()/1024.0)));
        System.out.println("Current memory usage in kilobytes :"+currentMemory);
}

I think I can use this to get the maximum heap size too but I'm not sure if it's very reliable.

I currently use those command lines :

-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime

In order to get good information about gc activity, young generation, old and permanent. But I just can tell the current total heap space when collections occurs.

So is there a way to get the max heap space that have been alocated using other command lines?

回答1:

You should use MemoryMXBean, see the javadoc for details as it explains how to use it to get information on each MemoryPool (aka generation) and to get notifications when different events happen.