如果我从Java命令行中省略了-Xmxn选项,那么将使用默认值。根据Java文档
"在运行时根据系统配置选择默认值"
哪些系统配置设置会影响默认值?
如果我从Java命令行中省略了-Xmxn选项,那么将使用默认值。根据Java文档
"在运行时根据系统配置选择默认值"
哪些系统配置设置会影响默认值?
当前回答
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版本。
其他回答
欧内斯托是对的。根据他发布的链接[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 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版本。
在运行时根据系统配置选择默认值
看一下文档页
默认堆大小 除非在命令行上指定了初始和最大堆大小,否则它们将根据计算机上的内存量计算。
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设置为相同的值
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命令行选项