Java › Garbage Collection
How does Metaspace (replacing PermGen since Java 8) store class metadata, and what specific configuration and failure modes differ from the old PermGen model?
AdvancedAnswer
Metaspace stores class metadata (loaded class definitions, method bytecode, constant pools) in native memory OUTSIDE the regular Java heap (unlike its predecessor PermGen, which was part of the heap itself and prone to a notorious OutOfMemoryError: PermGen space under class-loading-heavy scenarios like frequent hot redeployment) -- since Metaspace uses native memory, it can grow dynamically up to the available system memory by default (unless explicitly capped via -XX:MaxMetaspaceSize), which mostly eliminated PermGen's most common failure mode, though a genuine classloader leak (classes/classloaders never becoming garbage collectible due to lingering references) can still exhaust Metaspace and cause an analogous OutOfMemoryError: Metaspace, just with a much higher practical ceiling before hitting it.
# Explicitly cap Metaspace size (otherwise limited only by available native/system memory)
java -XX:MaxMetaspaceSize=256m -jar app.jar
# Monitor current Metaspace usage
jstat -gcutil <pid> | awk '{print $6}' # M column shows metaspace utilization %Real-world example An application server performing frequent hot-redeployments of web applications (each redeployment potentially leaving behind an unreleased classloader if not implemented carefully) previously hit OutOfMemoryError: PermGen space regularly under the old model, but after upgrading to a modern JVM with Metaspace, the same underlying classloader leak now takes dramatically longer to actually exhaust available memory, buying more time to properly fix the root leak.