Java / JVM Architecture (Java21) Interview questions
Why is the Metaspace stored in native memory instead of the heap?
Moving class metadata out of the heap and into native memory decouples its size from the fixed heap boundaries that caused PermGen's tight, hard-to-tune limits.
Because Metaspace lives in native memory, it can grow and shrink dynamically based on actual class-loading demand, rather than requiring a developer to pre-guess a fixed -XX:MaxPermSize that either wasted memory or ran out under heavy class loading.
It also keeps class metadata management separate from the heap's generational garbage collection, so collecting class metadata is tied directly to a classloader itself becoming unreachable and collectible, rather than being swept alongside ordinary object garbage.
The trade-off is that Metaspace now competes with the rest of the process's native memory, such as thread stacks, the JIT code cache, and direct buffers, so it is not truly unlimited even though it has no hard default ceiling.
More Related questions...