Explaining Java Metaspace and PermGen
The user is asking about metaspace and permgen in Java, which is indeed technical. I think using headings will help organize my explanation better since it’s a moderately long response. I should include a concise comparison, differences, migration, tuning tips, and common errors with their fixes. It’s also good to provide code for JVM flags, so I’ll ensure to format that in triple backticks with the appropriate language specified. Keeping it all concise is key!## Overview
- PermGen (Permanent Generation): fixed-size heap region in HotSpot prior to Java 8 that stored class metadata, interned strings, and some static data. Subject to GC but not compacted; could cause OutOfMemoryError: PermGen space when many classes/loaders or large interned strings were used.
- Metaspace: replaces PermGen starting in Java 8. Stores class metadata in native (off-heap) memory by default, removing the fixed-size constraint and many tuning headaches. Can still be capped with flags to avoid unbounded native memory growth.
Key differences
- Location
- PermGen: part of JVM heap (managed by JVM GC).
- Metaspace: native (off-heap) memory allocated from OS.
- Default sizing
- PermGen: fixed max size (e.g., -XX:MaxPermSize).
- Metaspace: unbounded by default (limited by native memory), can be limited via -XX:MaxMetaspaceSize.
- Contents
- Both store class metadata; PermGen also stored interned strings and some static data. In Java 7+ (u40+), interned strings moved to heap; in Java 8 with Metaspace, interned strings are on Java heap.
- Failure mode
- PermGen: OutOfMemoryError: PermGen space.
- Metaspace: OutOfMemoryError: Metaspace (or unable to allocate native memory) if limits or native exhaustion reached.
- Tuning flags
- PermGen:
-XX:PermSize, -XX:MaxPermSize.
- Metaspace:
-XX:MetaspaceSize, -XX:MaxMetaspaceSize, -XX:MinMetaspaceFreeRatio, -XX:MaxMetaspaceFreeRatio.
Common causes of errors
- PermGen OOME: many dynamically generated classes (proxy frameworks, reflection, hot-redeploy), excessive string interning, classloader leaks.
- Metaspace OOME: classloader leaks or truly excessive class generation without an appropriate MaxMetaspaceSize, or overall native memory exhaustion.
How to diagnose
- Look at JVM error message (PermGen vs Metaspace).
- Use tools: jmap, jcmd, jstat, jvisualvm, Native memory tracking (NMT) for metaspace.
- Inspect classloader retention (heap dumps) to find leaks (e.g., webapp redeploy issues).
How to fix / mitigate
- Fix leaks: ensure classes and classloaders are collectible (remove static references, stop threads started by webapps, deregister JDBC drivers, shutdown Executors).
- Reduce class generation: reuse proxies, avoid excessive runtime code gen.
- Tune JVM:
- For PermGen (pre-Java 8):
-XX:PermSize=64m -XX:MaxPermSize=256m
- For Metaspace (Java 8+):
-XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m
- Monitor memory: set GC logging, use monitoring tools to observe growth over time.
Practical recommendation
- If running Java 8+, rely on Metaspace but set reasonable MaxMetaspaceSize in memory-constrained environments and fix classloader leaks rather than just increasing limits.
- If on Java 7 or earlier, increase MaxPermSize temporarily but plan to upgrade to Java 8+ and address root causes.