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

发布于 2024-10-12 05:23:28 字数 273 浏览 7 评论 0 原文

如果我在 Java 命令行中省略 -Xmxn 选项,则将使用默认值。根据 Java 文档

“默认值是在运行时根据系统配置选择的”

哪些系统配置设置会影响默认值?

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

"the default value is chosen at runtime based on system configuration"

What system configuration settings influence the default value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

梦里兽 2024-10-19 05:23:29

默认值是在运行时根据系统配置选择的

请查看文档 页面

默认堆大小

除非在命令行中指定了初始堆大小和最大堆大小,否则它们是根据计算机上的内存量计算的。

  1. 客户端 JVM 默认初始和最大堆大小:

    默认最大堆大小是物理内存的一半,最多为 192 兆字节 (MB),否则为物理内存的四分之一,最多为物理内存大小1 吉字节 (GB)。

  2. 服务器 JVM 默认初始和最大堆大小:

    在 32 位 JVM 上,如果有 4 GB 或更多物理内存,默认最大堆大小可达 1 GB在 64 位 JVM 上,如果有 128 GB 或更多物理内存,默认最大堆大小可达 32 GB

哪些系统配置设置会影响默认值?

您可以使用标志 -Xms(初始堆大小)和 -Xmx 指定初始堆大小和最大堆大小(最大堆大小)。如果您知道应用程序需要多少堆才能正常运行,则可以将 -Xms-Xmx 设置为相同的值

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

╭⌒浅淡时光〆 2024-10-19 05:23:29

许多参数会影响生成大小。下图说明了堆中提交空间和虚拟空间之间的区别。在虚拟机初始化时,堆的整个空间都被保留。保留空间的大小可以使用-Xmx选项指定。如果 -Xms 参数的值小于 -Xmx 参数的值,则并非所有保留的空间都会立即提交给虚拟机。未提交的空间在此图中标记为“虚拟”。堆的不同部分(永久代、终身代和年轻代)可以根据需要增长到虚拟空间的极限。

情况下,虚拟机会在每个集合中增大或缩小堆,以尝试将每个集合中的可用空间与活动对象的比例保持在特定范围内。该目标范围由参数 -XX:MinHeapFreeRatio=-XX:MaxHeapFreeRatio= 设置为百分比,总大小为下面以 -Xms 为界,上面以 -Xmx 为界。

参数默认值

MinHeapFreeRatio 40

MaxHeapFreeRatio 70

-Xms 3670k

-Xmx 64m

64 位系统上堆大小参数的默认值已扩大约 30%。这种增加是为了补偿 64 位系统上较大的对象大小。

使用这些参数,如果一代中的可用空间百分比低于 40%,则该代将扩展以保持 40% 的可用空间,直至该代允许的最大大小。同样,如果可用空间超过 70%,则将收缩该代,以便只有 70% 的空间是可用的,但须遵守该代的最小大小。

大型服务器应用程序通常会遇到这些默认值的两个问题。一是启动缓慢,因为初始堆很小,必须在许多主要集合上调整大小。更紧迫的问题是,对于大多数服务器应用程序来说,默认的最大堆大小过小。服务器应用程序的经验法则是:

  • 除非您遇到暂停问题,否则尝试授予尽可能多的内存
    可以到虚拟机。默认大小 (64MB) 通常太大
    小的。
  • 将 -Xms 和 -Xmx 设置为相同的值可提高可预测性
    从虚拟机中删除最重要的大小决策。
    但是,如果您执行以下操作,虚拟机将无法进行补偿
    一个糟糕的选择。
  • 一般来说,随着数量的增加,内存也随之增加。
    处理器,因为分配可以并行化。

    这里有完整文章< /p>

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.

enter image description here

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

指尖微凉心微凉 2024-10-19 05:23:28

在 Windows 上,您可以使用以下命令来查找运行应用程序的系统上的默认值。

java -XX:+PrintFlagsFinal -version | findstr HeapSize

查找选项 MaxHeapSize(对于 -Xmx)和 InitialHeapSize 对于 -Xms

在 Unix/Linux 系统上,您可以这样做

java -XX:+PrintFlagsFinal -version | grep HeapSize

,我相信结果输出以字节为单位。

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.

属性 2024-10-19 05:23:28

对于 Java SE 5:根据 垃圾收集器人体工程学 [甲骨文]

初始堆大小:

机器上的机器物理内存的 1/64 或某些中的较大者
合理的最低限度。在 J2SE 5.0 之前,
默认初始堆大小是
合理的最小值,其变化取决于
平台。你可以覆盖这个
默认使用 -Xms 命令行
选项。

最大堆大小:

物理内存的 1/4 或 1GB 中较小的一个。在 J2SE 5.0 之前,
默认最大堆大小为 64MB。
您可以使用以下命令覆盖此默认值
-Xmx 命令行选项。

更新:

正如 Tom Anderson 在评论中指出的,以上内容适用于服务器级计算机。来自5.0 JavaTM 虚拟机中的人体工程学

在J2SE平台5.0版本中
称为 a 的机器类别
服务器级机器已定义
作为一台机器

  • 2 个或更多物理处理器
  • 2 GB 或更多物理内存

32 位平台除外
运行 Windows 版本
操作系统。在所有其他方面
平台默认值为
与版本的默认值相同
1.4.2.

在J2SE平台版本1.4.2中通过
默认以下选择是
制作

  • 初始堆大小为 4 MB
  • 最大堆大小为 64 MB

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
南风起 2024-10-19 05:23:28

Java 8 的 Xmssize(最小堆大小)占用超过 1/64 物理内存,但少于 1/4 物理内存为您的 -Xmxsize(最大堆大小)。

您可以通过以下方式检查默认 Java 堆大小

Windows 中:

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

Linux 中:

java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

哪些系统配置设置会影响默认值?

机器的物理内存和内存Java 版本。

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.

谁的新欢旧爱 2024-10-19 05:23:28

这在 Java 6 update 18 中进行了更改。

假设我们有超过 1 GB 的物理内存(现在很常见),它始终是服务器虚拟机物理内存的 1/4。

This is changed in 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.

幽蝶幻影 2024-10-19 05:23:28

最后!

从 Java 8u191 开始,您现在拥有以下选项:

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

可用于将堆大小设置为可用物理 RAM 的百分比。 (这与安装的 RAM 减去内核使用的 RAM 相同)。

有关详细信息,请参阅 Java8 u191 发行说明。请注意,这些选项是在 Docker 标题下提到的,但事实上,无论您是在 Docker 环境中还是在传统环境中,它们都适用。

MaxRAMPercentage 的默认值为 25%。这是极其保守的。

我自己的规则:如果您的主机或多或少专用于运行给定的java应用程序,那么您可以毫无问题地大幅增加。如果您使用的是 Linux,仅运行标准守护进程,并且安装了大约 1 Gb 及以上的 RAM,那么我会毫不犹豫地将 75% 用于 JVM 堆。再次强调,这是可用 RAM 的 75%,而不是已安装 RAM 的 75%。剩下的是可能在主机上运行的其他用户态进程以及 JVM 需要的其他类型的内存(例如堆栈)。总而言之,这通常非常适合剩下的 25%。显然,如果安装的内存更多,75% 是一个越来越安全的选择。 (我希望 JDK 人员已经实现了一个可以指定梯子的选项)

设置 MaxRAMPercentage 选项如下所示:

java -XX:MaxRAMPercentage=75.0  ....

请注意,这些百分比值是“双精度”类型,因此您必须 用小数点指定它们。如果您使用“75”而不是“75.0”,则会出现一些奇怪的错误。

Finally!

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".

毅然前行 2024-10-19 05:23:28

XmsXmx 是 Java 虚拟机 (JVM) 的标志:

  • Xms: 初始和最小 JVM 堆大小
    • 格式-Xms[g|G|m|M|k|K]
    • 默认大小
      • -server 模式:可用物理内存的 25%,>=8MB 且 <= 64MB
      • -客户端模式:可用物理内存的 25%,>=8MB 且 <= 16MB
    • 典型尺寸
      • -Xms128M
      • -Xms256M
      • -Xms512M
    • 功能/效果
      • -> JVM 启动时分配 Xms 大小的内存
  • Xmx: maximum JVM heap size
    • 格式-Xmx[g|G|m|M|k|K]
    • 默认大小
      • <= R27.2
        • Windows:总物理内存的 75%,最多 1GB
        • Linux/Solaris:可用物理内存的 50%,最多 1GB
      • >= R27.3
        • Windows X64:总物理内存的 75%,最高 2GB
        • Linux/Solaris X64:可用物理内存的 50%,高达 2GB
        • Windows x86:总物理内存的 75%,最高 1GB
        • Linux/Solaris X86:可用物理内存的 50%,最多 1GB
    • 典型尺寸
      • -Xmx1g
      • -Xmx2084M
      • -Xmx4g
      • -Xmx6g
      • -Xmx8g
    • 功能/效果
      • -> JVM 允许使用最大 Xmx 大小的内存
        • 当超过Xmx时,会出现java.lang.OutOfMemoryError
          • 如何修复OutOfMemoryError
            • 超过 Xmx
              • 例如:从-Xmx4g-Xmx8g

更多详细信息

请参阅官方文档:-X 命令行选项

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

雪花飘飘的天空 2024-10-19 05:23:28

埃内斯托是对的。根据他发布的链接[1]:

更新了客户端 JVM 堆配置

在客户端 JVM 中...

  • 默认最大堆大小是物理内存的一半(物理内存大小不超过 192 MB),否则为物理内存的四分之一(物理内存大小不超过 1 GB)。

    例如,如果您的计算机有 128 MB 物理内存,则最大堆大小为 64 MB,大于或等于 1 GB 物理内存会导致最大堆大小为 256 MB。

  • JVM 实际上不会使用最大堆大小,除非您的程序创建了足够的对象来需要它。在 JVM 初始化期间分配的量要小得多,称为初始堆大小。 ...

  • ...
  • 服务器 JVM 堆配置工效现在与客户端相同,不同之处在于32 位 JVM 的默认最大堆大小为 1 GB,对应于 4 GB 的物理内存大小,并且64 位 JVM 为 32 GB,对应的物理内存大小为 128 GB。

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

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

风轻花落早 2024-10-19 05:23:28

对于 IBM JVM,命令如下:

java -verbose:sizes -version

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

For the IBM JVM, the command is the following:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文