How is the default max Java heap size determined?

前端 未结 10 1720
旧巷少年郎
旧巷少年郎 2020-11-22 03:50

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

\"the default v

相关标签:
10条回答
  • 2020-11-22 04:15

    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
    0 讨论(0)
  • 2020-11-22 04:15

    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

    0 讨论(0)
  • 2020-11-22 04: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

    0 讨论(0)
  • 2020-11-22 04:16

    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=<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

    0 讨论(0)
  • 2020-11-22 04:18

    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.

    0 讨论(0)
  • 2020-11-22 04: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

    0 讨论(0)
提交回复
热议问题