Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine MaxDirectMemorySize on a running JVM?

I have an application which uses DirectByteBuffers to store data, but I'd like to know what MaxDirectMemorySize is so I don't accidentally exceed it.

Without configuring this manually, how can I figure out, from within the program, what MaxDirectMemorySize is?

like image 444
Li Pi Avatar asked Oct 17 '25 16:10

Li Pi


2 Answers

The accepted answer only works if the option is explicitly specified on the command line. As of Java 6, you can access the option directly using the HotSpotDiagnosticMXBean. The following Java 7 code can read it conveniently:

final HotSpotDiagnosticMXBean hsdiag = ManagementFactory
        .getPlatformMXBean(HotSpotDiagnosticMXBean.class);
if (hsdiag != null) {
    System.out.println(hsdiag.getVMOption("MaxDirectMemorySize"));
}

Note that this may return a value of zero, meaning to use the default setting, which is equal to Runtime.getRuntime().maxMemory(). For example, with Oracle JDK 7u71 64-bit on Windows 7, this returns 3,690,987,520.

Alternatively, if you're willing to resort to accessing the sun.misc package, it's available directly by calling sun.misc.VM.maxDirectMemory().

like image 179
Trevor Robinson Avatar answered Oct 20 '25 06:10

Trevor Robinson


Yuu can get ALL JVM parameters with...

RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
List<String> args=RuntimemxBean.getInputArguments();

for(int i=0;i<args.size();i++) {
    System.out.println(args.get(i)); 
}
like image 21
Andrew White Avatar answered Oct 20 '25 04:10

Andrew White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!