On Windows, you can use the following command to find out the defaults on the system where your applications runs.
java -XX:+PrintFlagsFinal -version | findstr HeapSize
Look for the options MaxHeapSize (for -Xmx) and InitialHeapSize for -Xms.
On a Unix/Linux system, you can do
java -XX:+PrintFlagsFinal -version | grep HeapSize
I believe the resulting output is in bytes.
Answer from stones333 on Stack OverflowOn Windows, you can use the following command to find out the defaults on the system where your applications runs.
java -XX:+PrintFlagsFinal -version | findstr HeapSize
Look for the options MaxHeapSize (for -Xmx) and InitialHeapSize for -Xms.
On a Unix/Linux system, you can do
java -XX:+PrintFlagsFinal -version | grep HeapSize
I believe the resulting output is in bytes.
For Java SE 5: According to Garbage Collector Ergonomics [Oracle]:
initial heap size:
Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum. Before J2SE 5.0, the default initial heap size was a reasonable minimum, which varies by platform. You can override this default using the -Xms command-line option.
maximum heap size:
Smaller of 1/4th of the physical memory or 1GB. Before J2SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.
UPDATE:
As pointed out by Tom Anderson in his comment, the above is for server-class machines. From Ergonomics in the 5.0 JavaTM Virtual Machine:
In the J2SE platform version 5.0 a class of machine referred to as a server-class machine has been defined as a machine with
- 2 or more physical processors
- 2 or more Gbytes of physical memory
with the exception of 32 bit platforms running a version of the Windows operating system. On all other platforms the default values are the same as the default values for version 1.4.2.
In the J2SE platform version 1.4.2 by default the following selections were made
- initial heap size of 4 Mbyte
- maximum heap size of 64 Mbyte
What is the default maximum heap size for Sun's JVM from Java SE 6? - Stack Overflow
How is the default maximum heap size of an Oracle Java 7 JVM calculated? - Stack Overflow
memory - What is the default max heap size (-Xmx) in Java 8? - Stack Overflow
garbage collection - Why default java max heap is 1/4th of Physical memory? - Stack Overflow
Videos
java 1.6.0_21 or later, or so...
$ java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSize
uintx MaxHeapSize := 12660904960 {product}
It looks like the min(1G) has been removed.
Or on Windows using findstr
C:\>java -XX:+PrintFlagsFinal -version 2>&1 | findstr MaxHeapSize
One can ask with some Java code:
long maxBytes = Runtime.getRuntime().maxMemory();
System.out.println("Max memory: " + maxBytes / 1024 / 1024 + "M");
See javadoc.
The answer was in your question only in the first line itself -
default maximum heap size should be "Smaller of 1/4th" of the physical memory.
In your case 1/4th of main memory is 24GB but heap size is 21GB, which is satisfying your first line statement.
To make it more clear run below code to get the actual main memory size
public class SizeOfMainMemory {
public static void main(String[] args) {
com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
System.out.println(mxbean.getTotalPhysicalMemorySize()/1024/1024);
}
}
You will find your HEAP SIZE is 1/4th of your main memory or may be little less.
OK I might have found an answer.
That linked page mention java "SE-5.0" and makes no reference to "64-bit" perhaps it was oudated?
Here's an updated equivalent:
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html
with a new link in it "For initial heap and maximum heap sizes for 64-bit systems, see the section Default Heap Size in The Parallel Collector."
On that page: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size
It states "On 64-bit JVMs, the default maximum heap size can be up to 32 GB if there is 128 GB or more of physical memory."
But the same seems to be accurate for JDK 7 so I'm thinking that link was outdated...
It varies on implementation and version, but usually it depends on the VM used (e.g. client or server, see -client and -server parameters) and on your system memory.
Often for client the default value is 1/4th of your physical memory or 1GB (whichever is smaller).
Also Java configuration options (command line parameters) can be "outsourced" to environment variables including the -Xmx, which can change the default (meaning specify a new default). Specifically the JAVA_TOOL_OPTIONS environment variable is checked by all Java tools and used if exists (more details here and here).
You can run the following command to see default values:
java -XX:+PrintFlagsFinal -version
It gives you a loooong list, -Xmx is in MaxHeapSize, -Xms is in InitialHeapSize. Filter your output (e.g. |grep on linux) or save it in a file so you can search in it.
Like you have mentioned, The default -Xmxsize (Maximum HeapSize) depends on your system configuration.
Java8 client takes Larger of 1/64th of your physical memory for your Xmssize (Minimum HeapSize) and Smaller of 1/4th of your physical memory for your -Xmxsize (Maximum HeapSize).
Which means if you have a physical memory of 8GB RAM, you will have Xmssize as Larger of 8*(1/64) and Smaller of -Xmxsizeas 8*(1/4).
You can Check your default HeapSize with
In Windows:
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
In Linux:
java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
These default values can also be overrided to your desired amount.