Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

HotSpot removed PermGen in JDK 8 and replaced it with metaspace in native memory, and later made metaspace return memory to the OS more promptly. Why was this change made, what did it fix and not fix, and what lesson does it hold for running JVMs in containers today?

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.

What each step changed
ReleaseChangeEffect
JDK 7Interned strings and statics moved to the heapLess pressure on PermGen
JDK 8PermGen removed, metaspace in native memoryNo fixed size, little tuning needed
JDK 16Elastic metaspaceLess fragmentation, memory returned to the OS
UnchangedClass loader leaksNow 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.

Java
// A classic class loader leak: a static registry outlives a redeployed plugin
public final class Registry {
static final List<Object> HANDLERS = new ArrayList<>(); // loaded by the parent loader
}
// Plugin code, loaded by a child class loader on each reload:
Registry.HANDLERS.add(new PluginHandler()); // keeps PluginHandler's class and its loader alive
// After 50 reloads with no MaxMetaspaceSize: metaspace 2.1 GB and growing
// With -XX:MaxMetaspaceSize=512m: java.lang.OutOfMemoryError: Metaspace

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.

Step throughHow a class loader leak ends
  1. Class loader leak grows metaspaceOld loaders stay reachable
  2. MaxMetaspaceSize set?Guard in the runtime profile
    YesOutOfMemoryError: Metaspace, clear and diagnosableNoNative memory keeps growing
  3. Container limit reachedRSS crosses the cgroup limit
  4. 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.

What to say in the interviewAbout 90 seconds aloud

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/