How is the default max Java heap size determined?
Asked Answered
E

10

545

If I omit the -Xmxn option from the Java command line then a default value will be used. According to Java documentation

"the default value is chosen at runtime based on system configuration"

What system configuration settings influence the default value?

Eskisehir answered 12/1, 2011 at 10:6 Comment(3)
system configuration means: a) client jvm vs server jvm b) 32bit vs 64bit. Links: 1) updation from J2SE5.0 docs.oracle.com/javase/6/docs/technotes/guides/vm/… 2) brief answer: docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/… 3) detailed answer: docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/… 4) client vs server: javacodegeeks.com/2011/07/jvm-options-client-vs-server.htmlAlcoran
its tough to understand from the above links. So summarizing them here: Maximum heap size for Client jvm is 256mb (there is an exception, read from links above). Maximun 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 its 256mb or 1gb or 32gbAlcoran
See also https://mcmap.net/q/47396/-what-is-the-default-max-heap-size-xmx-in-java-8Merth
N
645

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.

Nels answered 14/12, 2012 at 1:52 Comment(10)
I was hoping for a nice option like this, but it didn't work for me using IBM's Java 6 VM.Grownup
Great! Can I play with all these default options? What's the corresponding ENV variable for each?Lyricist
In my case on Linux, InitialHeapSize = 262803264 and MaxHeapSize = 4206886912 which is about 256 MB and 4 GB if I'm not mistaken. Does this mean that every JVM starts as if it was launched with -Xms256m -Xmx4g options?Elka
On a Windows system: java -XX:+PrintFlagsFinal -version | findstr /R /C:"HeapSize"Kimberli
Kind of a partial answer only to the original question as asked, but very very helpful! I think the -version part is not necessary though.Hayrack
So... what does 0 mean? :)Molly
@matanster On my Linux -version suppresses the long "usage" text on stderr.Halftimbered
i get 'java not recognized as internal command' error. at what folder in the command line do you have to be at for this to work.Skeie
The Unix version does indeed work on macOS 10.14 on 64-bit Intel Mac mini with 32-gigs of memory running Java 15 from AdoptOpenJDK. Shows MaxHeapSize = 8589934592 and MinHeapSize = 8388608.Indian
grep HeapSize | numfmt --to=iec --field=4 will show human-readable sizesHotspur
B
129

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
Bandit answered 12/1, 2011 at 10:21 Comment(5)
Caveat: that's for server-class machines, not client-class. You need to read that document in conjunction with java.sun.com/docs/hotspot/gc5.0/ergo5.html which defines those terms and what happens to client-class machines. dogbane, might i humbly suggest you edit your answer to quote the relevant passages?Helenahelene
That is a ridiculously low default in 2012. Very few serious applications will fit inside 64 megabytes.Caerphilly
See Ernesto's Oct 30, 2012 answer for client machines after Java 6 update 18.Lepidus
Also keep in mind that it says: "The boundaries and fractions given for the heap size are correct for J2SE 5.0. They are likely to be different in subsequent releases as computers get more powerful."Reseta
By the way, this algo is only for Parallel Garbage Collector.Resumption
U
79

Java 8 takes more than 1/64th of your physical memory for your Xmssize (Minimum HeapSize) and less than 1/4th of your physical memory for your -Xmxsize (Maximum HeapSize).

You can check the default Java heap size by:

In Windows:

java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"

In Linux:

java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

What system configuration settings influence the default value?

The machine's physical memory & Java version.

Unbolt answered 12/4, 2017 at 11:47 Comment(5)
isn't it 1/64 instead of 1/6?Alcoran
Yeah Xmssize (Minimum HeapSize / InitialHeapSize) is more than 1/64th of your physical memory & Xmxsize (Maximum HeapSize / MaxHeapSize) is less than 1/4th of your physical memory . (For-ex for my mac , having 16GB ram , I am getting uintx InitialHeapSize := 268435456 {product} uintx MaxHeapSize := 4294967296 {product} , i,e Xms is 268 MB & Xmx is 4.29 GBStrangle
The values are in Bytes, if you want MB divide by 1024*1024Idette
This has really saved me lot of time, thanks a lot :)Sielen
is any way to set heap size over then 1/4th physical memory?Flaunch
W
43

This is changed in Java 6 update 18.

Assuming that we have more than 1 GB of physical memory (quite common these days), it's always 1/4th of your physical memory for the server vm.

Wade answered 30/10, 2012 at 6:42 Comment(5)
Incorrect, the linked page says greater than or equal to 1 gigabyte of physical memory results in a maximum heap size of 256 megabytesCalebcaledonia
I just checked on a linux machine with 5gb physical memory. Default max heap shows as 1.5gbWade
@PaoloFulgoni no, another practical example I observe right now: 129 Gbytes of physical memory results in 32 Gbyte of max heap sizeThorley
For more info on why this is the case see https://mcmap.net/q/46216/-how-is-the-default-max-java-heap-size-determined See also https://mcmap.net/q/47396/-what-is-the-default-max-heap-size-xmx-in-java-8Merth
The section quoted by @PaoloFulgoni refers to a client VM. The examples by @Wade and @Thorley are likely a server VM which is the default configuration when you're using the JDK VM as opposed to JRE.Phenothiazine
M
37

Finally!

As of Java 8u191 you now have the options:

-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage

that can be used to size the heap as a percentage of the usable physical RAM. (which is same as the RAM installed less what the kernel uses).

See Release Notes for Java8 u191 for more information. Note that the options are mentioned under a Docker heading but in fact they apply whether you are in Docker environment or in a traditional environment.

The default value for MaxRAMPercentage is 25%. This is extremely conservative.

My own rule: If your host is more or less dedicated to running the given java application, then you can without problems increase dramatically. If you are on Linux, only running standard daemons and have installed RAM from somewhere around 1 Gb and up then I wouldn't hesitate to use 75% for the JVM's heap. Again, remember that this is 75% of the RAM available, not the RAM installed. What is left is the other user land processes that may be running on the host and the other types of memory that the JVM needs (eg for stack). All together, this will typically fit nicely in the 25% that is left. Obviously, with even more installed RAM the 75% is a safer and safer bet. (I wish the JDK folks had implemented an option where you could specify a ladder)

Setting the MaxRAMPercentage option look like this:

java -XX:MaxRAMPercentage=75.0  ....

Note that these percentage values are of 'double' type and therefore you must specify them with a decimal dot. You get a somewhat odd error if you use "75" instead of "75.0".

Masoretic answered 11/9, 2019 at 19:28 Comment(5)
It seems that MinRAMPercentage parameter, unlike its name, allows setting the maximum heap size for a JVM running with a small amount of memory (less than around 250MB), and MaxRAMPercentage allows setting the maximum heap size for a JVM running with a large amount of memory (greater than around 250 MB). So the default values depend on the amount of RAM. The other value is ignored. You can get the default values with docker run --rm openjdk:8 java -XX:+PrintFlagsFinal -version | grep -E "RAMPercentage". MaxRAMPercentage = 25.0 and MinRAMPercentage = 50% on my laptop.Creese
these parameter is not working in ' traditional environment', i use the jdk8u333Flaunch
@sendreams: Did you remember to use decimal point when specifying the value?Masoretic
@Masoretic yes, i really use, but use visualvm connect the instance, the max heap size is always 16GBFlaunch
@Masoretic i have made a mistake, it can working now. tksFlaunch
G
19

The Xms and Xmx are flag of Java virtual machine (JVM):

  • Xms: initial and minimum JVM heap size
    • Format: -Xms<size>[g|G|m|M|k|K]
    • Default Size:
      • -server mode: 25% of free physical memory, >=8MB and <= 64MB
      • -client mode: 25% of free physical memory, >=8MB and <= 16MB
    • Typical Size:
      • -Xms128M
      • -Xms256M
      • -Xms512M
    • Function/Effect:
      • -> JVM start with allocate Xms size memory
  • Xmx: maximum JVM heap size
    • Format: -Xmx<size>[g|G|m|M|k|K]
    • Default Size:
      • <= R27.2
        • Windows: 75% of total physical memory up to 1GB
        • Linux/Solaris: 50% of available physical memory up to 1GB
      • >= R27.3
        • Windows X64: 75% of total physical memory up to 2GB
        • Linux/Solaris X64: 50% of available physical memory up to 2GB
        • Windows x86: 75% of total physical memory up to 1GB
        • Linux/Solaris X86: 50% of available physical memory up to 1GB
    • Typical Size:
      • -Xmx1g
      • -Xmx2084M
      • -Xmx4g
      • -Xmx6g
      • -Xmx8g
    • Function/Effect:
      • -> JVM allow use maxium of Xmx size memory
        • when exceed Xmx, will java.lang.OutOfMemoryError
          • How to fix OutOfMemoryError ?
            • exceed Xmx value
              • eg: from -Xmx4g to -Xmx8g

More detail

see official doc: -X Command-line Options

Gamester answered 11/4, 2019 at 10:31 Comment(1)
Isn't that for the JRockit JVM? (as opposed to Oracle's Hotspot JVM)Masoretic
T
16

Ernesto is right. According to the link he posted [1]:

Updated Client JVM heap configuration

In the Client JVM...

  • The default maximum heap size is half of the physical memory up to a physical memory size of 192 megabytes and otherwise one fourth of the physical memory up to a physical memory size of 1 gigabyte.

    For example, if your machine has 128 megabytes of physical memory, then the maximum heap size is 64 megabytes, and greater than or equal to 1 gigabyte of physical memory results in a maximum heap size of 256 megabytes.

  • The maximum heap size is not actually used by the JVM unless your program creates enough objects to require it. A much smaller amount, termed the initial heap size, is allocated during JVM initialization. ...

  • ...
  • Server JVM heap configuration ergonomics are now the same as the Client, except that the default maximum heap size for 32-bit JVMs is 1 gigabyte, corresponding to a physical memory size of 4 gigabytes, and for 64-bit JVMs is 32 gigabytes, corresponding to a physical memory size of 128 gigabytes.

[1] http://www.oracle.com/technetwork/java/javase/6u18-142093.html

Teodoor answered 9/11, 2012 at 15:22 Comment(0)
M
10

For the IBM JVM, the command is the following:

java -verbose:sizes -version

For more information about the IBM SDK for Java 8: http://www-01.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.lnx.80.doc/diag/appendixes/defaults.html?lang=en

Mashie answered 3/9, 2015 at 9:39 Comment(3)
The link seems to be (effectively) broken.Inviolate
@PeterMortensen just checked - link is fineMashie
@NikHandyman Broken now: We're sorry! Content not foundDianthus
T
8

default value is chosen at runtime based on system configuration

Have a look at the documentation page

Default Heap Size

Unless the initial and maximum heap sizes are specified on the command line, they are calculated based on the amount of memory on the machine.

  1. Client JVM Default Initial and Maximum Heap Sizes:

    The default maximum heap size is half of the physical memory up to a physical memory size of 192 megabytes (MB) and otherwise one fourth of the physical memory up to a physical memory size of 1 gigabyte (GB).

  2. Server JVM Default Initial and Maximum Heap Sizes:

    On 32-bit JVMs, the default maximum heap size can be up to 1 GB if there is 4 GB or more of physical memory. 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

What system configuration settings influence the default value?

You can specify the initial and maximum heap sizes using the flags -Xms (initial heap size) and -Xmx (maximum heap size). If you know how much heap your application needs to work well, you can set -Xms and -Xmx to the same value

Tellus answered 13/10, 2016 at 16:5 Comment(0)
O
6

A number of parameters affect generation size. The following diagram illustrates the difference between committed space and virtual space in the heap. At initialization of the virtual machine, the entire space for the heap is reserved. The size of the space reserved can be specified with the -Xmx option. If the value of the -Xms parameter is smaller than the value of the -Xmx parameter, not all of the space that is reserved is immediately committed to the virtual machine. The uncommitted space is labeled "virtual" in this figure. The different parts of the heap (permanent generation, tenured generation and young generation) can grow to the limit of the virtual space as needed.

enter image description here

By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range. This target range is set as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and -XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms<min> and above by -Xmx<max>.

Parameter Default Value

MinHeapFreeRatio 40

MaxHeapFreeRatio 70

-Xms 3670k

-Xmx 64m

Default values of heap size parameters on 64-bit systems have been scaled up by approximately 30%. This increase is meant to compensate for the larger size of objects on a 64-bit system.

With these parameters, if the percent of free space in a generation falls below 40%, the generation will be expanded to maintain 40% free space, up to the maximum allowed size of the generation. Similarly, if the free space exceeds 70%, the generation will be contracted so that only 70% of the space is free, subject to the minimum size of the generation.

Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications. The rules of thumb for server applications are:

  • Unless you have problems with pauses, try granting as much memory as possible to the virtual machine. The default size (64MB) is often too small.
  • Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the virtual machine. However, the virtual machine is then unable to compensate if you make a poor choice.
  • In general, increase the memory as you increase the number of processors, since allocation can be parallelized.

    There is the full article

Optometrist answered 6/2, 2019 at 10:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.