JavaGuide icon indicating copy to clipboard operation
JavaGuide copied to clipboard

JVM 参数 -XX:MetaspaceSize 解释错误

Open qtoo opened this issue 2 years ago • 1 comments

JVM 参数章节 jvm-parameters-intro.md: L98 中提到:

-XX:MetaspaceSize=N #设置 Metaspace 的初始(和最小大小)

此处解释错误,即 Metaspace 的初始大小非由 -XX:MetaspaceSize 设置。


参见 Oracle 官方文档 Other Considerations 中提到:

When the space committed for class metadata reaches a certain level (a high-water mark), a garbage collection is induced.

The high-water mark is initially set to the value of the command-line option MetaspaceSize.

可见 -XX:MetaspaceSize 是一个提前设定的阈值,会触发 GC,那一定不是初始大小。


通过实验证实 Metaspace 的初始大小非由 -XX:MetaspaceSize 设置,而是会引起 Full GC,过程详见:

JVM 参数 MetaspaceSize 的误解

qtoo avatar Mar 09 '23 09:03 qtoo

参见 Oracle 官方文档 Other Considerations 中提到:

When the space committed for class metadata reaches a certain level (a high-water mark), a garbage collection is induced.

The high-water mark is initially set to the value of the command-line option MetaspaceSize.

可见 -XX:MetaspaceSize 是一个提前设定的阈值,会触发 GC,那一定不是初始大小。

感谢指出,已经修正。

1、Metaspace 的初始容量并不是 -XX:MetaspaceSize 设置,无论 -XX:MetaspaceSize 配置什么值,对于 64 位 JVM 来说,Metaspace 的初始容量都是 21807104(约 20.8m)。

可以参考 Oracle 官方文档 Other Considerations中提到的:

Specify a higher value for the option MetaspaceSize to avoid early garbage collections induced for class metadata. The amount of class metadata allocated for an application is application-dependent and general guidelines do not exist for the selection of MetaspaceSize. The default size of MetaspaceSize is platform-dependent and ranges from 12 MB to about 20 MB.

MetaspaceSize 的默认大小取决于平台,范围从 12 MB 到大约 20 MB。

另外,还可以看一下这个试验:JVM 参数 MetaspaceSize 的误解

2、Metaspace 由于使用不断扩容到-XX:MetaspaceSize参数指定的量,就会发生 FGC,且之后每次 Metaspace 扩容都会发生 Full GC。

也就是说,MetaspaceSize 表示 Metaspace 使用过程中触发 Full GC 的阈值,只对触发起作用。

垃圾搜集器内部是根据变量 _capacity_until_GC来判断 Metaspace 区域是否达到阈值的,初始化代码如下所示:

void MetaspaceGC::initialize() {
  // Set the high-water mark to MaxMetapaceSize during VM initializaton since
  // we can't do a GC during initialization.
  _capacity_until_GC = MaxMetaspaceSize;
}

Snailclimb avatar Mar 12 '23 01:03 Snailclimb