如果我从Java命令行中省略了-Xmxn选项,那么将使用默认值。根据Java文档
"在运行时根据系统配置选择默认值"
哪些系统配置设置会影响默认值?
如果我从Java命令行中省略了-Xmxn选项,那么将使用默认值。根据Java文档
"在运行时根据系统配置选择默认值"
哪些系统配置设置会影响默认值?
当前回答
Xms和Xmx是Java虚拟机(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
更详细地
查看官方文档:-X命令行选项
其他回答
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 6更新18中有所改变。
假设我们有超过1gb的物理内存(现在很常见),它总是服务器vm的物理内存的1/4。
终于!
从Java 8u191开始,你现在有以下选项:
-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage
可用于按可用物理RAM的百分比来确定堆的大小。(这与安装的RAM比内核使用的少)。
有关更多信息,请参阅Java8 u191的发布说明。请注意,这些选项是在Docker标题下提到的,但实际上无论您是在Docker环境中还是在传统环境中,它们都适用。
MaxRAMPercentage的默认值是25%。这是极端保守的。
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)
设置MaxRAMPercentage选项如下所示:
java -XX:MaxRAMPercentage=75.0 ....
请注意,这些百分比值是“double”类型,因此必须使用小数点指定它们。如果使用“75”而不是“75.0”,就会得到一个有点奇怪的错误。
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版本。
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.
默认情况下,虚拟机会增加或缩小每个集合的堆,以尽量将每个集合的空闲空间与活动对象的比例保持在特定的范围内。该目标范围由参数-XX:MinHeapFreeRatio=<minimum>和-XX:MaxHeapFreeRatio=<maximum>设置为百分比,总大小的下限为-Xms<min>,上限为-Xmx<max>。
参数默认值
MinHeapFreeRatio 40
MaxHeapFreeRatio 70
xms 3670 k
- xmx 64
64位系统上堆大小参数的默认值增加了大约30%。这种增加是为了弥补64位系统中对象的较大大小。
使用这些参数,如果代中的空闲空间百分比低于40%,则将扩展代以保持40%的空闲空间,直到允许的最大代大小。同样,如果自由空间超过70%,则代将收缩,以只有70%的空间是自由的,这取决于代的最小大小。
大型服务器应用程序使用这些默认值时经常遇到两个问题。一个是启动缓慢,因为初始堆很小,必须在许多主要集合中调整大小。一个更紧迫的问题是,对于大多数服务器应用程序来说,默认的最大堆大小小得不合理。服务器应用程序的经验法则是:
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