Quick revision
The short answer
PermGen was a fixed-size area, so applications with many classes or frequent redeploys ran into OutOfMemoryError: PermGen space and needed manual tuning; metaspace removed that tuning by sizing itself on demand, and JDK 16's elastic metaspace cut fragmentation and returned unused memory to the OS. It did not fix class loader leaks: the failure simply moved from a clear heap-area error to unbounded native growth. In containers, unbounded native areas end as kernel OOM kills, so the lesson is to make defaults safe and to put guards such as MaxMetaspaceSize and memory monitoring into a standard runtime profile.
What to remember
In this answer, we saw that PermGen's fixed size forced manual tuning and failed with OutOfMemoryError: PermGen space, and that metaspace removed most tuning by growing on demand, with JDK 16's elastic metaspace returning unused memory. Class loader leaks were not fixed; they moved from a clear error to unbounded native growth, which in containers ends as a kernel OOM kill. The lesson is to keep self-sizing defaults but add guard limits and monitoring in a standard runtime profile.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
PermGen was a fixed-size area, so applications with many classes or frequent redeploys ran into OutOfMemoryError: PermGen space and needed manual tuning; metaspace removed that tuning by sizing itself on demand, and JDK 16's elastic metaspace cut fragmentation and returned unused memory to the OS. It did not fix class loader leaks: the failure simply moved from a clear heap-area error to unbounded native growth. In containers, unbounded native areas end as kernel OOM kills, so the lesson is to make defaults safe and to put guards such as MaxMetaspaceSize and memory monitoring into a standard runtime profile.
Why this question is asked
The removal of PermGen is often told as a simple success story: an annoying error went away. The fuller story is more useful for anyone who sets platform standards, because it shows how a good change can move a failure somewhere less visible. Interviewers ask this at a senior level to see whether we can draw lessons from platform history and apply them to how we run JVMs today. In this answer, we will look at what PermGen got wrong, what metaspace and elastic metaspace fixed, what they did not fix, and what that means for JVMs in containers.
1What Went Wrong With PermGen
PermGen, the permanent generation, held class metadata, interned strings and static fields in an area with a fixed maximum, MaxPermSize, often 64 or 256 MB. The right value depended on how many classes an application loaded, which nobody knew in advance, and which grew with every library added.
Application servers made it worse. Each redeploy of a web application created a new class loader and loaded all its classes again. If anything still referenced the old class loader, such as a thread started by the application or a JDBC driver registered globally, the old classes could not be unloaded. After a few redeploys, the server failed with OutOfMemoryError: PermGen space, and the usual reaction was to raise MaxPermSize and restart more often.
2What Metaspace and Elastic Metaspace Fixed
JDK 8 removed PermGen and moved class metadata into metaspace, in native memory, where it grows on demand. Interned strings and static fields had already moved to the heap in JDK 7. For most applications this removed the need to tune anything: an application with twice as many classes simply used twice as much metaspace.
The first metaspace implementation had its own weakness. It allocated memory in chunks per class loader, and after classes were unloaded, freed chunks often stayed fragmented and committed, so metaspace rarely shrank. JDK 16 replaced the allocator with elastic metaspace, which uses a buddy-style chunk allocator, merges free chunks and uncommits memory it no longer needs, returning it to the operating system much more promptly. Applications that load and unload many classes saw their native footprint drop.
| Release | Change | Effect |
|---|---|---|
| JDK 7 | Interned strings and statics moved to the heap | Less pressure on PermGen |
| JDK 8 | PermGen removed, metaspace in native memory | No fixed size, little tuning needed |
| JDK 16 | Elastic metaspace | Less fragmentation, memory returned to the OS |
| Unchanged | Class loader leaks | Now grow native memory instead of failing early |
3What It Did Not Fix
A class loader leak is still a leak. If an old class loader stays reachable, every class it loaded stays in metaspace. The difference is what happens next. With PermGen, the fixed limit was reached quickly and the JVM threw a clear error naming the area. With metaspace and no MaxMetaspaceSize, there is no limit inside the JVM: metadata keeps growing in native memory until something outside the JVM stops it.
Notice that the cap does not prevent the leak. It makes the leak fail early, inside the JVM, with an error that names the area, which is exactly what a person on call needs.
4Why Containers Make This Matter More
On a physical server, unbounded native growth eventually pushes the machine into swap and becomes slow. In a container, the cgroup memory limit is hard. When the process crosses it, the kernel kills it with SIGKILL: exit code 137, OOMKilled in Kubernetes, no Java exception, no heap dump, no log line. The team sees restarts and has to guess.
Metaspace is only one of these unbounded or loosely bounded areas. Direct buffers, thread stacks, the code cache, GC structures and memory used by native libraries all count against the same limit, and most of them do not show up in heap graphs. So the PermGen story generalises: removing a fixed limit improves the common case, but without a guard, the rare case becomes a silent kill.
- Class loader leak grows metaspaceOld loaders stay reachable
- MaxMetaspaceSize set?Guard in the runtime profileYesOutOfMemoryError: Metaspace, clear and diagnosableNoNative memory keeps growing
- Container limit reachedRSS crosses the cgroup limit
- Kernel OOM killExit 137, no Java error, restart loop
4 steps in order. Play them, or step through them one at a time.
5The Lesson for Platform Standards
Platform defaults should be safe by default. Self-sizing is good, but every area that can grow should have an upper bound that turns runaway growth into a clear Java error, and the total should fit inside the container limit with measured headroom. In practice, a standard runtime profile in the shared base image sets MaxMetaspaceSize well above normal use, MaxDirectMemorySize, a bounded code cache, and a heap percentage of the limit. It also exports metrics for metaspace, non-heap pools and resident memory, and alerts before the limit is reached.
The second lesson is about how we judge platform changes. When a change removes a failure, we should ask where the failure went. Often it moved to a place we monitor less.
6Follow-up questions
Which other JVM memory areas need explicit budgets in a container?
Direct buffer memory, capped with MaxDirectMemorySize; thread stacks, which we control through the number of threads and -Xss; the code cache, bounded by ReservedCodeCacheSize; GC structures, which scale with heap size and the collector; the compressed class space; and native memory from libraries and malloc arenas, which the JVM does not limit at all and which we bound through measurement and settings such as MALLOC_ARENA_MAX. The budget should list each one with its measured size.
Is setting MaxMetaspaceSize risky?
Only if it is set close to normal use, because legitimate growth after a new release could then fail. Set it at two to three times the level the service reaches after warmup, and review it when a service adds large frameworks.
How do we find a class loader leak once the guard trips?
Take a heap dump and look for many instances of the same class loader type, then follow their paths to GC roots. The path usually ends in a static field, a thread or a registry that a parent class loader owns.
7Conclusion
In this answer, we saw that PermGen's fixed size forced manual tuning and failed with OutOfMemoryError: PermGen space, and that metaspace removed most tuning by growing on demand, with JDK 16's elastic metaspace returning unused memory. Class loader leaks were not fixed; they moved from a clear error to unbounded native growth, which in containers ends as a kernel OOM kill. The lesson is to keep self-sizing defaults but add guard limits and monitoring in a standard runtime profile.
Open with
PermGen was a fixed-size area for class metadata, so its right size depended on how many classes an app loaded, which nobody knew. Apps with many libraries, and especially app servers that redeployed web apps with a class loader leak, hit OutOfMemoryError PermGen space and lived on hand-tuned MaxPermSize and restarts.
Then, if they want more
JDK 8 replaced it with metaspace in native memory, which sizes itself on demand, so most tuning disappeared. The first implementation fragmented and rarely gave memory back, and JDK 16's elastic metaspace fixed that by merging free chunks and returning memory to the OS. What it did not fix is class loader leaks.
The leak still happens; it just stopped hitting a small, clearly named limit and started growing native memory without bound. On a server that means slow swapping, but in a container it means the kernel kills the process with exit code 137 and there is no Java error at all.
My lesson for today is that defaults should be safe by default. A self-sizing area still needs a guard, so our standard runtime profile sets MaxMetaspaceSize well above normal use, along with limits on direct memory and the code cache, a heap percentage of the limit, and alerts on resident memory and OOMKilled restarts. And whenever a change removes a failure, I ask where that failure moved.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 7, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "HotSpot removed PermGen in JDK 8 and replaced it with metaspace in native memory, and...": https://scrivio-interview-library.netlify.app/java/memory-layout/fb2bc40ce2/