How is the default max Java heap size determined?
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 32gb
10 Answers 10
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.
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?
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.
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:
- 2 or more physical processors
- 2 or more Gbytes of physical memory
- initial heap size of 4 Mbyte
- maximum heap size of 64 Mbyte
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?
That is a ridiculously low default in 2012. Very few serious applications will fit inside 64 megabytes.
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.»
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"
java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
What system configuration settings influence the default value?
The machine’s physical memory & Java version.
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
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.
Incorrect, the linked page says greater than or equal to 1 gigabyte of physical memory results in a maximum heap size of 256 megabytes
@PaoloFulgoni no, another practical example I observe right now: 129 Gbytes of physical memory results in 32 Gbyte of max heap size
The section quoted by @PaoloFulgoni refers to a client VM. The examples by @ernesto and @Kirill are likely a server VM which is the default configuration when you’re using the JDK VM as opposed to JRE.
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».
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.
The Xms and Xmx are flag of Java virtual machine (JVM):
- Xms : initial and minimum JVM heap size
- Format : -Xms[g|G|m|M|k|K]
- Default Size :
- -server mode: 25% of free physical memory, >=8MB and
- -client mode : 25% of free physical memory, >=8MB and
- -Xms128M
- -Xms256M
- -Xms512M
- -> JVM start with allocate Xms size memory
- Format : -Xmx[g|G|m|M|k|K]
- Default Size :
- Windows : 75% of total physical memory up to 1GB
- Linux/Solaris : 50% of available physical memory up to 1GB
- 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
- -Xmx1g
- -Xmx2084M
- -Xmx4g
- -Xmx6g
- -Xmx8g
- -> 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
Ernesto is right. According to the link he posted [1]:
- 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.
For the IBM JVM, the command is the following:
java -verbose:sizes -version
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.
- 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).
- 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
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.
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= and -XX:MaxHeapFreeRatio= , and the total size is bounded below by -Xms and above by -Xmx .
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
- exceed Xmx value
- How to fix OutOfMemoryError ?
- when exceed Xmx , will java.lang.OutOfMemoryError