views:

765

answers:

3

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?

+3  A: 

Try this ones:

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

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

Arne Burmeister
Note that Non-Heap memory also includes the Code Cache, which is separate from Permanent Generation. (at least in Sun's JVM)
Joshua McKinnon
A: 

As for eclipse, try looking for a profiling tool. I think NetBeans has one by default.

pek
I'm trying to do this from a program so I can give users a warning if they use less than my plugin will require.
Uri
I misunderstood your question. I think this is a better answer.
pek
+1  A: 

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.

Neil Coffey