简体   繁体   English

如何确定默认的最大 Java 堆大小?

[英]How is the default max Java heap size determined?

If I omit the -Xmx n option from the Java command line then a default value will be used.如果我从 Java 命令行中省略-Xmx n选项,则将使用默认值。 According to Java documentation根据Java文档

"the default value is chosen at runtime based on system configuration" “默认值是在运行时根据系统配置选择的”

What system configuration settings influence the default value?哪些系统配置设置会影响默认值?

On Windows, you can use the following command to find out the defaults on the system where your applications runs.在 Windows 上,您可以使用以下命令找出运行应用程序的系统上的默认值。

java -XX:+PrintFlagsFinal -version | java -XX:+PrintFlagsFinal -version | findstr HeapSize findstr 堆大小

Look for the options MaxHeapSize (for -Xmx ) and InitialHeapSize for -Xms .查找选项MaxHeapSize (用于-Xmx )和InitialHeapSize用于-Xms

On a Unix/Linux system, you can do在 Unix/Linux 系统上,你可以做

java -XX:+PrintFlagsFinal -version | java -XX:+PrintFlagsFinal -version | grep HeapSize grep 堆大小

I believe the resulting output is in bytes.我相信结果输出以字节为单位。

For Java SE 5: According to Garbage Collector Ergonomics [Oracle] :对于 Java SE 5:根据Garbage Collector Ergonomics [Oracle]

initial heap size:初始堆大小:

Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum.机器上机器物理内存的 1/64 或某个合理的最小值中的较大者。 Before J2SE 5.0, the default initial heap size was a reasonable minimum, which varies by platform.在 J2SE 5.0 之前,默认的初始堆大小是合理的最小值,因平台而异。 You can override this default using the -Xms command-line option.您可以使用 -Xms 命令行选项覆盖此默认值。

maximum heap size:最大堆大小:

Smaller of 1/4th of the physical memory or 1GB.物理内存的 1/4 或 1GB,以较小者为准。 Before J2SE 5.0, the default maximum heap size was 64MB.在 J2SE 5.0 之前,默认的最大堆大小是 64MB。 You can override this default using the -Xmx command-line option.您可以使用 -Xmx 命令行选项覆盖此默认值。

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 :来自5.0 JavaTM 虚拟机中的人体工程学

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在 J2SE 平台版本 5.0 中,称为服务器级机器的一类机器被定义为具有

  • 2 or more physical processors 2 个或更多物理处理器
  • 2 or more Gbytes of physical memory 2 GB 或更多的物理内存

with the exception of 32 bit platforms running a version of the Windows operating system.除了运行 Windows 操作系统版本的 32 位平台。 On all other platforms the default values are the same as the default values for version 1.4.2.在所有其他平台上,默认值与版本 1.4.2 的默认值相同。

In the J2SE platform version 1.4.2 by default the following selections were made在 J2SE 平台版本 1.4.2 中,默认情况下进行了以下选择

  • initial heap size of 4 Mbyte初始堆大小为 4 MB
  • maximum heap size of 64 Mbyte最大堆大小为 64 MB

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).爪哇8需要的物理内存超过1/64为您Xmssize(最小HEAPSIZE)和您的-Xmxsize(最大HEAPSIZE)的物理内存小于1/4。

You can check the default Java heap size by:您可以通过以下方式检查默认 Java 堆大小

In Windows :Windows 中

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

In Linux :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.机器的物理内存和 Java 版本。

This is changed in Java 6 update 18 .这在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.假设我们有超过1 GB的物理内存(现在很常见),它总是服务器虚拟机物理内存的 1/4。

Finally!最后!

As of Java 8u191 you now have the options:从 Java 8u191 开始,您现在可以选择:

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

that can be used to size the heap as a percentage of the usable physical RAM.可用于将堆大小确定为可用物理 RAM 的百分比。 (which is same as the RAM installed less what the kernel uses). (这与安装的 RAM 少内核使用的内容相同)。

See Release Notes for Java8 u191 for more information.有关更多信息,请参阅Java8 u191 的发行说明 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.请注意,这些选项在 Docker 标题下提到,但实际上,无论您是在 Docker 环境中还是在传统环境中,它们都适用。

The default value for MaxRAMPercentage is 25%. MaxRAMPercentage的默认值为 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.我自己的规则:如果你的主机或多或少致力于运行给定的java应用程序,那么你可以毫无问题地大幅增加。 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.如果您在 Linux 上,只运行标准守护进程,并且已经安装了大约 1 Gb 及以上的 RAM,那么我会毫不犹豫地将 75% 用于 JVM 的堆。 Again, remember that this is 75% of the RAM available , not the RAM installed .再次记住,这是可用RAM 的 75%,而不是安装的 RAM。 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).剩下的是可能在主机上运行的其他用户进程和 JVM 需要的其他类型的内存(例如用于堆栈)。 All together, this will typically fit nicely in the 25% that is left.总之,这通常很适合剩下的 25%。 Obviously, with even more installed RAM the 75% is a safer and safer bet.显然,安装更多 RAM 时,75% 是一个更安全的赌注。 (I wish the JDK folks had implemented an option where you could specify a ladder) (我希望 JDK 人员实现了一个可以指定梯子的选项)

Setting the MaxRAMPercentage option look like this:设置MaxRAMPercentage选项如下所示:

java -XX:MaxRAMPercentage=75.0  ....

Note that these percentage values are of 'double' type and therefore you must specify them with a decimal dot.请注意,这些百分比值为“double”类型,因此您必须使用小数点指定它们。 You get a somewhat odd error if you use "75" instead of "75.0".如果你使用“75”而不是“75.0”,你会得到一个有点奇怪的错误。

Ernesto is right.埃内斯托是对的。 According to the link he posted [1]:根据他发布的链接 [1]:

Updated Client JVM heap configuration更新了客户端 JVM 堆配置

In the Client JVM...在客户端 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.默认的最大堆大小是物理内存的一半,最大为 192 兆字节的物理内存大小,否则为物理内存的四分之一,最大为 1 GB 的物理内存大小。

    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.例如,如果您的机器有 128 兆字节的物理内存,则最大堆大小为 64 兆字节,大于或等于 1 GB 的物理内存导致最大堆大小为 256 兆字节。

  • The maximum heap size is not actually used by the JVM unless your program creates enough objects to require it. JVM 不会实际使用最大堆大小,除非您的程序创建了足够多的对象来要求它。 A much smaller amount, termed the initial heap size, is allocated during JVM initialization.在 JVM 初始化期间分配的数量要小得多,称为初始堆大小。 ... ...

  • ... ...
  • 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.服务器 JVM 堆配置人机工程学现在与客户端相同,除了32 位 JVM 的默认最大堆大小为 1 GB ,对应的物理内存大小为 4 GB,而64 位 JVM的默认最大堆大小为32 GB ,对应到 128 GB 的物理内存大小。

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

The Xms and Xmx are flag of Java virtual machine (JVM): XmsXmx是 Java 虚拟机 (JVM) 的标志:

  • Xms : initial and minimum JVM heap size Xmsinitial and minimum JVM heap size
    • Format : -Xms<size>[g|G|m|M|k|K] Format-Xms<size>[g|G|m|M|k|K]
    • Default Size : Default Size
      • -server mode: 25% of free physical memory, >=8MB and <= 64MB -server模式:25% 的可用物理内存,>=8MB 且 <= 64MB
      • -client mode : 25% of free physical memory, >=8MB and <= 16MB -client mode :25% 的可用物理内存,>=8MB 和 <=16MB
    • Typical Size : Typical Size
      • -Xms128M
      • -Xms256M
      • -Xms512M
    • Function / Effect : Function / Effect
      • -> JVM start with allocate Xms size memory -> JVM 开始分配Xms大小的内存
  • Xmx : maximum JVM heap size Xmx : maximum JVM heap size
    • Format : -Xmx<size>[g|G|m|M|k|K] Format-Xmx<size>[g|G|m|M|k|K]
    • Default Size : Default Size
      • <= R27.2
        • Windows : 75% of total physical memory up to 1GB Windows :总物理内存的75% ,最高可达1GB
        • Linux/Solaris : 50% of available physical memory up to 1GB Linux/Solaris :可用物理内存的50% ,最高可达1GB
      • >= R27.3
        • Windows X64 : 75% of total physical memory up to 2GB Windows X64 :总物理内存的75% ,最高可达2GB
        • Linux/Solaris X64 : 50% of available physical memory up to 2GB Linux/Solaris X64 :可用物理内存的50% ,最高可达2GB
        • Windows x86 : 75% of total physical memory up to 1GB Windows x86 :总物理内存的75% ,最高可达1GB
        • Linux/Solaris X86 : 50% of available physical memory up to 1GB Linux/Solaris X8650%的可用物理内存,高达1GB
    • Typical Size : Typical Size
      • -Xmx1g
      • -Xmx2084M
      • -Xmx4g
      • -Xmx6g
      • -Xmx8g
    • Function / Effect : Function / Effect
      • -> JVM allow use maxium of Xmx size memory -> JVM 允许使用最大Xmx大小的内存
        • when exceed Xmx , will java.lang.OutOfMemoryError当超过Xmx ,会出现java.lang.OutOfMemoryError
          • How to fix OutOfMemoryError ?如何修复OutOfMemoryError
            • exceed Xmx value超过Xmx
              • eg: from -Xmx4g to -Xmx8g例如:从-Xmx4g-Xmx8g

More detail更多详情

see official doc: -X Command-line Options请参阅官方文档: -X 命令行选项

For the IBM JVM, the command is the following:对于 IBM JVM,命令如下:

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有关 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

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:客户端 JVM 默认初始和最大堆大小:

    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) .默认的最大堆大小是物理内存的一半,最大为 192 兆字节 (MB) 的物理内存大小,否则为物理内存的四分之一,最大为 1 吉字节 (GB) 的物理内存大小

  2. Server JVM Default Initial and Maximum Heap Sizes:服务器 JVM 默认初始和最大堆大小:

    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 .在 32 位 JVM 上,如果有 4 GB 或更多的物理内存则默认的最大堆大小可以高达 1 GB 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在 64 位 JVM 上,如果有 128 GB 或更多的物理内存,则默认的最大堆大小可以达到 32 GB

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).您可以使用标志-Xms (初始堆大小)和-Xmx (最大堆大小)指定初始和最大堆大小。 If you know how much heap your application needs to work well, you can set -Xms and -Xmx to the same value如果您知道您的应用程序需要多少堆才能正常工作,您可以将-Xms-Xmx设置为相同的值

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.保留空间的大小可以使用-Xmx选项指定。 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.如果-Xms参数的值小于-Xmx参数的值,则并非所有保留的空间都会立即提交给虚拟机。 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> .此目标范围由参数 - XX:MinHeapFreeRatio=<minimum>-XX:MaxHeapFreeRatio=<maximum>设置为百分比,并且总大小的范围低于-Xms<min>和高于-Xmx<max> .

Parameter Default Value参数默认值

MinHeapFreeRatio 40最小堆自由比率 40

MaxHeapFreeRatio 70 MaxHeapFreeRatio 70

-Xms 3670k -Xms 3670k

-Xmx 64m -Xmx 64m

Default values of heap size parameters on 64-bit systems have been scaled up by approximately 30%. 64 位系统上堆大小参数的默认值已扩大了大约 30%。 This increase is meant to compensate for the larger size of objects on a 64-bit system.这种增加是为了补偿 64 位系统上更大的对象大小。

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.使用这些参数,如果一个代中的可用空间百分比低于 40%,则该代将被扩展以保持 40% 的可用空间,直到该代的最大允许大小。 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.同样,如果空闲空间超过 70%,则代将收缩,使得只有 70% 的空间是空闲的,受限于代的最小大小。

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.默认大小 (64MB) 通常太小。
  • Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the virtual machine.将 -Xms 和 -Xmx 设置为相同的值可以通过从虚拟机中删除最重要的大小决定来提高可预测性。 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全文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM