For JVM after Java 8
-XX:metaspaceSize
, it will trigger a gc.-XX:metaspaceSize
and -XX:maxMetaspaceSize
, the initial size of metaspace is usually a fixed value (20.8M) on a 64-bit server. -XX:metaspaceSize
is 20G for example, the current size of the metaspace is 18M and a large number of new objects (about 100M) must be allocated, the JVM must resize the metaspace for these new objects, will JVM triggers a full GC before resizing?First of all, "the size of the metaspace" is ambiguous, and thus meaningless without the context. There are at least five metrics: reserved, committed, capacity and used memory as described in this answer, and the high-water mark, also known as capacity_until_gc.
Metaspace is not just one contiguous region of memory, so it does not resize in the common sense. Instead, when allocation happens, one or more of the above metrics changes.
used
memory increases in this case, and that's it.capacity
increases. No GC happens until this point.committed
size would exceed capacity_until_gc
.capacity_until_gc
threshold is reached, JVM triggers a GC cycle.After GC, the high-water mark value is adjusted basing on the following JVM flags:
-XX:MinMetaspaceFreeRatio
(used to calculate how much free space is desirable in the metaspace capacity to decide how much to increase the HWM);-XX:MaxMetaspaceFreeRatio
(used to decide how much free space is desirable in the metaspace capacity before decreasing the HWM);-XX:MinMetaspaceExpansion
(the minimum expansion of Metaspace in bytes);-XX:MaxMetaspaceExpansion
(the maximum expansion of Metaspace without full GC).TL;DR It's not that simple. JVM can definitely commit more Metaspace memory without triggering GC. However, when HWM is reached, GC is triggered and HWM is recomputed according to the ergonomics policy.