Reputation: 33745
In my case this value is usually different than -Xmx
that I'm specifying.
For -Xmx810M
it returned 821035008
which is 783MB
. According to various (not truly reliable) sources it should return exactly the value specified by -Xmx
parameter.
I look for:
-Xmx
maxMemory()
is not accurateUpvotes: 3
Views: 1066
Reputation: 533880
Runtime.maxMemory() is accurate. It is the -Xmx
option which is not. How inaccurate it is depends on the version and I not sure what it is more of a hint than a precise setting.
When the totalMemory() matches your maxMemory() you are out of memory.
Upvotes: 1
Reputation: 116
According to https://bugs.java.com/bugdatabase/view_bug?bug_id=4391499 (which is from 2001, so it may have changed in newer versions of the JVM):
"The interpretation of the -Xmx flag is VM-dependent. Some VMs, including HotSpot, enforce a lower bound on the effective value of this option."
So as the previous answer stated, MaxMemory is accurate, and the Xmx flag is simply a suggestion.
The link above was found via an answer to an earlier similar question on SO.
And if you want to find the user specified value of Xmx, you could simply do roughly the following if using a Sun (ahem, Oracle) JVM/:
String commandLineInput = System.getProperty("sun.java.command");
Pattern pattern = Pattern.compile("-Xmx(\\w*)");
Matcher matcher = pattern.matcher(commandLineInput);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
Upvotes: 6