Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the max sizes of the heap and permgen from the JVM?

I am trying to find out programatically the max permgen and max heap size with which a the JVM for my program has been invoked, not what is currently available to them.

Is there a way to do that?

I am familiar with the methods in Java Runtime object, but its not clear what they really deliver.

Alternatively, is there a way to ask Eclipse how much was allocated for these two?

like image 620
Uri Avatar asked Jan 11 '09 18:01

Uri


People also ask

How to get the heap size of a running Java application?

1. Overview In this quick tutorial, we're going to get familiar with a few different ways to get the heap size of a running Java application. To find the heap and metaspace related info of a running Java application, we can use the jcmd command-line utility: First, let's find the process id of a particular Java application using the jps command:

What is the use of PermGen in Java?

PermGen contains meta-data of the classes and the objects i.e. pointers into the rest of the heap where the objects are allocated. The PermGen also contains Class-loaders which have to be manually destroyed at the end of their use else they stay in memory and also keep holding references to their objects on the heap.

What is the heap space in Java?

Java Heap space: Java objects are instantiations of Java classes. Our JVM has an internal representation of those Java objects and those internal representations are stored in the heap. This Java heap memory is divided again into regions, called generations. Eden Space : When object created using new keyword memory allocated on this space.

What is Eden space in JVM?

Our JVM has an internal representation of those Java objects and those internal representations are stored in the heap. This Java heap memory is divided again into regions, called generations. Eden Space : When object created using new keyword memory allocated on this space. Newly created objects are usually located in this space.


2 Answers

Try something like this for max perm gen:

public static long getPermGenMax() {
    for (MemoryPoolMXBean mx : ManagementFactory.getMemoryPoolMXBeans()) {
        if ("Perm Gen".equals(mx.getName())) {
            return mx.getUsage().getMax();
        }
    }
    throw new RuntimeException("Perm gen not found");
}

For max heap, you can get this from Runtime, though you can also use the appropriate MemoryPoolMXBean.

like image 66
Neil Coffey Avatar answered Oct 26 '22 23:10

Neil Coffey


Try this ones:

MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
mem.getHeapMemoryUsage().getUsed();
mem.getNonHeapMemoryUsage().getUsed();

But they only offer snapshot data, not a cummulated value.

like image 25
Arne Burmeister Avatar answered Oct 27 '22 00:10

Arne Burmeister