1

Possible Duplicate:
Read Java JVM startup parameters (eg -Xmx)

We have a desktop application where some old installations does not have the appropriate memory settings.

They experience out of perm gen space at "random" times and i need to alert the users which are running too low MaxPermSize but i can't figure out how to access this setting from the jvm.

Community
  • 1
  • 1
user1035344
  • 195
  • 2
  • 2
  • 11
  • 1
    Finding out what the "-XX:MaxPermSize" setting is does not tell you how much permgen space is actually used. So this approach only helps if you can accurately predict how much permgen space *should* be used by the application. – Stephen C Nov 14 '12 at 10:31

3 Answers3

4

This should give you a rough idea on how to do it

public class Main {
    public static void main(final String[] args) {
        final RuntimeMXBean memMXBean = ManagementFactory.getRuntimeMXBean();
        final String prefix = "-XX:MaxPermSize=";
        final List<String> jvmArgs = memMXBean.getInputArguments();
        String maxPermSize = null;
        for (final String jvmArg : jvmArgs) {
            if (jvmArg.startsWith(prefix)) {
                maxPermSize = jvmArg.substring(prefix.length());
                break;
            }
        }

        System.out.println("MaxPermSize is " + maxPermSize);
    }
}
ShyJ
  • 4,560
  • 1
  • 19
  • 19
1

First I suggest you to connect JConsole or VisualVM.There is no way for you to determine the PermGen size in your application(programmatically). Another way is to use MemoryMXBean but it do simple memory reporting, but stick to using the tools I mentioned to get a more detailed picture.
So if you are going for MemoryMXBean then Try this code

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

It show you snapshot data not the commulated values :)

Freak
  • 6,786
  • 5
  • 36
  • 54
  • This won't really let me get MaxPermSpace only as you say a snapshot of what is used right now. I need to check the parameter during startup for an desktop application =( – user1035344 Nov 14 '12 at 10:29
-1

this will give the respective property value.

System.getProperty("XX:MaxPermSize"); 

check this url How can I view the MaxPermSize in JVM?

Community
  • 1
  • 1
  • 1
    This does not work, there is no system property available as far as i can tell. I printed all my system properties http://pastie.org/5376306 launching with -XX:MaxPermSize=192m as a argument java -jar "C:\Java\hack\MemoryTest\dist\MemoryTest.jar" -XX:MaxPermSize=192m – user1035344 Nov 14 '12 at 10:22
  • The url you provided has nothing to do with your sollution which does not work. – JayTheKay Feb 25 '16 at 10:30