Test program (TestFreeMemory.java):
public class TestFreeMemory {
public static void main(String[] args){
long maxMem = Runtime.getRuntime().maxMemory()/(1024*1024);
System.out.println("Max Mem: " + maxMem);
long freeMem = Runtime.getRuntime().freeMemory()/(1024*1024);
System.out.println("Free Mem: " + freeMem);
long totalMem = Runtime.getRuntime().totalMemory()/(1024*1024);
System.out.println("Total Mem: " + totalMem);
}
}
$ javac TestFreeMemory.java $ java -Xms32m -Xmx500m TestFreeMemory Max Mem: 444 Free Mem: 30 Total Mem: 30
$ java -Xms64m -Xmx500m TestFreeMemory Max Mem: 444 Free Mem: 61 Total Mem: 61
$ java -Xms64m -Xmx1024m TestFreeMemory Max Mem: 910 Free Mem: 61 Total Mem: 61
$ java -Xms32m -Xmx2048m TestFreeMemory Max Mem: 1820 Free Mem: 30 Total Mem: 30
You can see (444/500)=(910/1024)=(1820/2048), so Runtime.getRuntime().maxMemory() is determined by "-Xmx", which presents the upper limit of java process. The Runtime.getRuntime().totalMemory() is the initial memory capacity, which is determined by "-Xms" , it will grow when needed by program, until to "-Xmx".
$ java -Xms32m -Xmx20480m TestFreeMemory Max Mem: 18204 Free Mem: 30 Total Mem: 30
$ free -m total used free shared buffers cached Mem: 15937 15759 178 0 248 3068 -/+ buffers/cache: 12443 3494 Swap: 8039 641 7398
The upper limit of physical memory is 15GB, while I declared 20GB max memory for java process. This shows that the "-Xmx" is a nominal value, which means java won't check its realizability when startup.