Java 堆默认大小

最近在调整程序的堆大小设置,就顺便看了开发环境中如何设置的。发现开发环境没有主动设置大小,那就应该是根据机器配置和操作系统以及JDK版本等信息使用了默认配置。

查询 PrintFlagsFinal

关于查询命令,请参考官方文档:
-XX:+PrintFlagsFinal which outputs the values of all of the jvm configuration parameters and/or values

个人笔记本上:Windows10 ,JDK 64-Bit Server VM (build 25.191-b12) 物理内存16G

C:\Users\Lenovo>java -XX:+PrintFlagsFinal -version |findstr /i "HeapSize  PerSize ThreadStackSize"
     intx CompilerThreadStackSize                   = 0                                   {pd product}
    uintx ErgoHeapSizeLimit                         = 0                                   {product}
    uintx HeapSizePerGCThread                       = 87241520                            {product}
    uintx InitialHeapSize                          := 266338304                           {product}
    uintx LargePageHeapSizeThreshold                = 134217728                           {product}
    uintx MaxHeapSize                              := 4253024256                          {product}
     intx ThreadStackSize                           = 0                                   {pd product}
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
     intx VMThreadStackSize                         = 0                                   {pd product}

也就是在我的笔记本上默认的MaxHeapSize 是4253024256/1024/1024/1024=3.96G., 初始堆大小是InitialHeapSize 266338304 /1024/1024=254M.

使用-XX:+PrintCommandLineFlags

关于PringCommandLineFlags的解释,请参考https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html#BABHDABI

-XX:+PrintCommandLineFlags

    Enables printing of ergonomically selected JVM flags that appeared on the command line. It can be useful to know the ergonomic values set by the JVM, such as the heap space size and the selected garbage collector. By default, this option is disabled and flags are not printed.

执行结果如下:

C:\Users\Lenovo>java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=265755648 -XX:MaxHeapSize=4252090368 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

可以看出-XX:+PrintCommandLineFlags与-XX:+PrintFlagsFinal结果一样MaxHeapSize=4253024256基本一致(暂未找到差异觉具体解释)

使用java -XshowSettings:all

可以发现>java -XshowSettings:all现实的最大内存与PrintFlagsFinal现实的有所差异。(暂未找到具体解释)

C:\Users\Lenovo>java -XshowSettings:all
VM settings:
    Max. Heap Size (Estimated): 3.52G
    Ergonomics Machine Class: client
    Using VM: Java HotSpot(TM) 64-Bit Server VM

Property settings:
    awt.toolkit = sun.awt.windows.WToolkit
    file.encoding = GBK
    。。。

Unix上执行
java -XX:+PrintFlagsFinal -version | grep -iE ‘HeapSize|PermSize|ThreadStackSize’

官方文档

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

截图其中的

-Xmssize  (设置堆的初始大小)

    Sets the initial size (in bytes) of the heap. This value must be a multiple of 1024 and greater than 1 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

    The following examples show how to set the size of allocated memory to 6 MB using various units:

    -Xms6291456
    -Xms6144k
    -Xms6m

    If you do not set this option, then the initial size will be set as the sum of the sizes allocated for the old generation and the young generation. The initial size of the heap for the young generation can be set using the -Xmn option or the -XX:NewSize option.
-Xmxsize  (设置堆的最大大小)

    Specifies the maximum size (in bytes) of the memory allocation pool in bytes. This value must be a multiple of 1024 and greater than 2 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. The default value is chosen at runtime based on system configuration. **For server deployments, -Xms and -Xmx are often set to the same value**(对于服务部署,通常-Xms和-Xmx设置的大小一样). See the section "Ergonomics" in Java SE HotSpot Virtual Machine Garbage Collection Tuning Guide at http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/index.html.

    The following examples show how to set the maximum allowed size of allocated memory to 80 MB using various units:

    -Xmx83886080
    -Xmx81920k
    -Xmx80m

    The -Xmx option is equivalent to -XX:MaxHeapSize


Initial Heap Size and Maximum Heap Size Changed for Parallel Garbage Collector
On server-class machines running either VM (client or server) with the parallel garbage collector (-XX:+UseParallelGC) the initial heap size and maximum heap size have changed as follows.

initial heap size(默认初始大小, 物理内存的64分之一,或者合理的最小值。 我的机器物理内存是16G, 因此默认就是0.25G, 也就是256M)
Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum. Before Java SE 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(默认最大堆, 物理内存的四分之一,或者1G。 我机器物理内存16G, 默认最大值就是4G)
Smaller of 1/4th of the physical memory or 1GB. Before Java SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.

Note: The boundaries and fractions given for the heap size are correct for Java SE 5.0. They are likely to be different in subsequent releases as computers get more powerful.

官方文档连接 https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html

你可能感兴趣的:(Java,JVM)