7

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?

Uri
  • 88,451
  • 51
  • 221
  • 321

3 Answers3

8

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.

divanov
  • 6,173
  • 3
  • 32
  • 51
Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • 1
    Be careful as if you activate CMS the MX bean name is "CMS Perm Gen". You can replace the "Perm Gen".equals(mx.getName()) by mx.getName().endsWith("Perm Gen") to make it work in that case too. – Laurent Grégoire Feb 08 '12 at 11:06
  • Don't know what it's depending on but for me this is "PS Perm Gen". :-) – Jan Goyvaerts May 25 '12 at 09:40
7

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
  • 20,046
  • 8
  • 53
  • 94
0

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

pek
  • 17,847
  • 28
  • 86
  • 99
  • 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 Jan 11 '09 at 19:34
  • I misunderstood your question. I think this is a better answer. – pek Jan 11 '09 at 19:57