As we all know, the java -Xmx
option is used to set the maximum heap space available to a Java program. But is there a way for a Java program to read the value that
There is a Runtime.maxMemory, which the documentation says will return the maximum amount of memory the Java Virtual Machine will attempt to use, but it is not specific whether it is the maximum heap memory set when launching the JVM.
Just as a test, I wrote the following program:
class MaxMemory {
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().maxMemory());
}
}
The results of the execution was the following:
C:\coobird\>java -Xmx64m MaxMemory
66650112
C:\coobird\>java -Xmx128m MaxMemory
133234688
This is using Java SE 6 version 1.6.0_12 on Windows.
Although the values are close to 64 MB (67,108,864 bytes) and 128 MB (134,217,728 bytes), they aren't exactly so.