Releasing Metaspace memory
One of the major changes from PermGen (pre-Java 8) to Metaspace (Java 8 onwards) is that the Metaspace can now grow in size. By default, the amount of memory allocated for the Metaspace is unbounded, as it is part of native memory. The size of the Metaspace can be customized using the JVM –
XX:MetaspaceSize
flag.
The Metaspace can trigger garbage collection in only two scenarios:
- Metaspace runs out of memory
- Metaspace size exceeds a JVM-set threshold
Let us examine these in turn.
Metaspace runs out of memory
As stated, by default, the native memory available to the Metaspace is unlimited. If you run out of memory, you get an OutOfMemoryError
message, and this will trigger a run of the garbage collector. You can limit the Metaspace size with the JVM –XX:MaxMetaspaceSize
flag. If you reach this limit, that will also trigger a run of the garbage collector.
Metaspace size exceeds a JVM-set threshold
We can configure...