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

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

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


当前回答

欧内斯托是对的。根据他发布的链接[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

其他回答

这在Java 6更新18中有所改变。

假设我们有超过1gb的物理内存(现在很常见),它总是服务器vm的物理内存的1/4。

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

java -XX:+PrintFlagsFinal -version | findstr HeapSize

寻找选项MaxHeapSize(用于-Xmx)和InitialHeapSize(用于-Xms)。

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

java -XX:+PrintFlagsFinal -version | grep HeapSize

我认为结果输出是以字节为单位的。

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

看一下文档页

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

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

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

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

Java SE 5:根据垃圾收集器工效学[Oracle]:

初始堆大小: 大于机器物理内存的1/64 合理的最低。在J2SE 5.0之前, 默认的初始堆大小为 合理的最小值,由 平台。你可以重写这个 默认使用-Xms命令行 选择。 最大堆大小: 小于物理内存的1/4或1GB。在J2SE 5.0之前, 默认最大堆大小为64MB。 您可以使用 -Xmx命令行选项。

更新:

正如Tom Anderson在他的评论中所指出的,以上是针对服务器级机器的。来自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 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

Java 8的Xmssize(最小堆大小)占用物理内存的1/64以上,-Xmxsize(最大堆大小)占用物理内存的1/4以下。

你可以通过以下方法检查默认的Java堆大小:

在Windows中:

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

在Linux中:

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

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

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