83

In the oracle documentation I found:

-Xmxsize Specifies the maximum size (in bytes) of the memory allocation pool in bytes ... The default value is chosen at runtime based on system configuration.

What does system configuration mean?

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
Vitaly
  • 2,292
  • 1
  • 16
  • 21

5 Answers5

146

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.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
icza
  • 289,344
  • 42
  • 658
  • 630
  • 12
    As a small addition, you can run `java -XX:+PrintCommandLineFlags` to print out the heap sizes (and other information) chosen by the JVM based on current system information – Cristian Vat Feb 02 '15 at 08:00
  • 1
    @CristianVat Yes, but the parameter is `-XX:+PrintFlagsFinal`, the one you suggested does not work for me. Adding it to the answer. – icza Feb 02 '15 at 08:04
  • Right, sorry `-XX:+PrintFlagsFinal` is the best option since it should show all information after everything has been taken into account (including manual options and ergonomics). Although `-XX:+PrintCommandLineFlags` seems to work on my JVM (might depend on exact version) – Cristian Vat Feb 02 '15 at 08:06
  • 2
    For large boxes, this "1/4th RAM" rule of thumb definitely does not hold. On a 4-socket, 64gb per socket server (i.e. 256gb RAM), Xmx defaults to ~32gb. 32gb may be related to CompressedOops' limitations being at around this point, too. – FauxFaux Nov 12 '15 at 09:12
  • 2
    This only print heap size related lines: `java -XX:+PrintFlagsFinal -version | grep HeapSize` – user218867 Jan 18 '17 at 08:15
  • Is there a way to change the 1/4th ratio value itself instead of the Xmx value? It would help in a containerised scenario with `-XX:+UseCGroupMemoryLimitForHeap` flag on. – xmar Mar 20 '19 at 16:38
  • @icza I believe 1/4th corresponds to client jvm and 1gb corresponds to server jvm of 4gb/more of physical memory. And 32gb for server jvm of 128gb/more physical memory. Kindly correct me if I'm wrong – Vyshnav Ramesh Thrissur May 08 '19 at 09:30
  • @VyshnavRameshThrissur It may be. Do you have sources (link) to confirm? – icza May 09 '19 at 11:37
  • @icza i have posted it as an answer below. Kindly have a look at it. https://stackoverflow.com/a/56036202/5649620 – Vyshnav Ramesh Thrissur May 09 '19 at 13:50
38

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.

A Nice Guy
  • 2,538
  • 3
  • 27
  • 50
Sarat Chandra
  • 4,192
  • 23
  • 27
  • 2
    Reference: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size – kisna Jan 20 '18 at 02:31
  • https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html as per this link, default for minimum/initial is 1/64 not 1/6. "Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum" – Vyshnav Ramesh Thrissur May 08 '19 at 03:17
15

Surprisingly this question doesn't have a definitive documented answer. Perhaps another data point would provide value to others looking for an answer. On my systems running CentOS (6.8,7.3) and Java 8 (build 1.8.0_60-b27, 64-Bit Server):

default memory is 1/4 of physical memory, not limited by 1GB.

Also, -XX:+PrintFlagsFinal prints to STDERR so command to determine current default memory presented by others above should be tweaked to the following:

java -XX:+PrintFlagsFinal 2>&1 | grep MaxHeapSize

The following is returned on system with 64GB of physical RAM:

uintx MaxHeapSize                                  := 16873684992      {product}
brianNotBob
  • 419
  • 3
  • 5
2

On my Ubuntu VM, with 1048 MB total RAM, java -XX:+PrintFlagsFinal -version | grep HeapSize printed : uintx MaxHeapSize := 266338304, which is approx 266MB and is 1/4th of my total RAM.

Binita Bharati
  • 2,998
  • 28
  • 20
  • 2
    It's looking to me like OpenJDK and Oracle have different characteristics - I see OpenJDK using 1/4 of RAM as -Xmx at all times (never the smaller of 1/4 and 1GB) – pogul Aug 20 '18 at 09:46
2

As of 8, May, 2019:

JVM heap size depends on system configuration, meaning:

a) client jvm vs server jvm

b) 32bit vs 64bit.

Links:

1) updation from J2SE5.0: https://docs.oracle.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html
2) brief answer: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html
3) detailed answer: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size
4) client vs server: https://www.javacodegeeks.com/2011/07/jvm-options-client-vs-server.html

Summary: (Its tough to understand from the above links. So summarizing them here)

1) Default maximum heap size for Client jvm is 256mb (there is an exception, read from links above).

2) Default maximum heap size for Server jvm of 32bit is 1gb and of 64 bit is 32gb (again there are exceptions here too. Kindly read that from the links).

So default maximum jvm heap size is: 256mb or 1gb or 32gb depending on VM, above.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332