What is the default max heap size (-Xmx) in Java 8?
Asked Answered
D

5

100

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?

Dulse answered 2/2, 2015 at 7:29 Comment(9)
It means that if your system has 16 GB of RAM, the size will be bigger than if it has 512 MB.Pamphleteer
Thanks, how much bigger?Dulse
Look for "maximum heap size" hereSwanger
>>Smaller of 1/4th of the physical memory or 1GB. Thanks, @alfasin.Dulse
oracle.com/technetwork/java/ergo5-140223.htmlMosa
@Dulse The 1Gb limit seems don't hold for -Xmx, at least in Java 8, because on my linux 64bit system, with total memory as 6Gb, I have a jvm has -Xmx = 1.5Gb, by default.Hairsplitter
@Dulse kindly note that your answer is for Java SE5. From SE8, maximum heap size is 256mb by default for client jvm.Sunup
@Hairsplitter Wang you might have used server jvm instead of client jvm. Server jvm can have 1gb or 32 gb max heap size by default depending on the ramSunup
Does this answer your question? How is the default max Java heap size determined?Dover
K
172

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.

Kaleighkalends answered 2/2, 2015 at 7:42 Comment(11)
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 informationCoccidiosis
@CristianVat Yes, but the parameter is -XX:+PrintFlagsFinal, the one you suggested does not work for me. Adding it to the answer.Kaleighkalends
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)Coccidiosis
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.Astrophysics
This only print heap size related lines: java -XX:+PrintFlagsFinal -version | grep HeapSizeHairsplitter
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.Multifold
@Kaleighkalends 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 wrongSunup
@VyshnavRameshThrissur It may be. Do you have sources (link) to confirm?Kaleighkalends
@Kaleighkalends i have posted it as an answer below. Kindly have a look at it. https://mcmap.net/q/47396/-what-is-the-default-max-heap-size-xmx-in-java-8Sunup
you also can use jinfo <pid_of_java> to get info from running process.Askja
Something is not quite right here. I just saw a client VM (which is the default if you install JRE instead of JDK). The default MapHeapSize is 256MB - on a Windows 16GB RAM machine. I think the answer of @VyshnavRameshThrissur is more accurate in this regard.Assignable
L
51

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.

Layette answered 12/4, 2017 at 12:3 Comment(2)
Reference: docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/…Eulogy
docs.oracle.com/javase/8/docs/technotes/guides/vm/… 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"Sunup
S
16

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}
Sharpset answered 27/1, 2017 at 16:43 Comment(3)
In my system with 16 Gb of RAM: 2069889024 = 2 Gb.Jerz
Windows 7 64bit with 8 GB of RAM: 32bit JVMs: 256 MB, 64bit JVMs: 2 GBGaytan
Finally found the documentation for it: https://mcmap.net/q/47396/-what-is-the-default-max-heap-size-xmx-in-java-8Dover
M
5

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.

Musty answered 27/4, 2018 at 13:8 Comment(1)
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)Permanence
S
5

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.

Sunup answered 8/5, 2019 at 8:7 Comment(1)
Finally links to real docu that explain what is going on.Dover

© 2022 - 2024 — McMap. All rights reserved.