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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章