Quick revision
The short answer
The JVM unloads a class only when its defining class loader becomes unreachable, which requires that no instances, Class objects or references to the loader remain reachable, and then only during a GC cycle that does class unloading: a full GC, or the concurrent marking cycles of G1 and ZGC. The bootstrap, platform and application loaders are referenced by the JVM itself for its whole life, so the classes they define are effectively never unloaded. Hidden classes that are not strongly tied to their loader are the exception and can be unloaded on their own.
What to remember
In this answer, we saw that a class can be unloaded only when its defining loader is unreachable, which means no reachable instances, Class objects or references to the loader, including hidden ones such as ThreadLocal values and registries. Unloading happens during full GCs and the concurrent cycles of G1 and ZGC, never in young collections. The bootstrap, platform and application loaders are always reachable, so their classes stay loaded for the life of the JVM, while hidden classes not strongly tied to their loader can be unloaded independently. -Xlog:class+unload and jcmd VM.classloader_stats show what is happening.
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.
The JVM unloads a class only when its defining class loader becomes unreachable, which requires that no instances, Class objects or references to the loader remain reachable, and then only during a GC cycle that does class unloading: a full GC, or the concurrent marking cycles of G1 and ZGC. The bootstrap, platform and application loaders are referenced by the JVM itself for its whole life, so the classes they define are effectively never unloaded. Hidden classes that are not strongly tied to their loader are the exception and can be unloaded on their own.
Why this question is asked
Class unloading is invisible until it stops working, and then it shows up as slowly rising metaspace, a growing class count and, eventually, OutOfMemoryError: Metaspace after a few redeploys. Interviewers ask this internals question to see whether we can reason about reachability at the level of loaders, and whether we know the tools that show what is going on. In this answer, we will recap how classes and loaders are linked, go through the exact conditions for unloading, see when the collectors actually unload, explain why the built-in loaders never unload, cover hidden classes, and then look at the diagnostic commands.
1A Quick Recap of Classes and Loaders
Each loaded class has metadata in metaspace, the native memory area for classes, and a java.lang.Class object on the heap. Each class points to its defining class loader, each loader keeps a list of the classes it defined, and every object points to its class. So from any single object of a class, we can reach its Class object, its loader and every other class that loader defined.
The JVM treats this whole group as one unit. Inside HotSpot, the metadata of all classes defined by one loader is kept together in that loader's own metaspace area, which is why freeing it per loader is cheap and freeing it per class is not supported.
2The Exact Conditions for Unloading
A class can be unloaded only when its loader is unreachable from the GC roots. In practice that means three things are all true at once: no instance of any class defined by the loader is reachable, no Class object of those classes is reachable, and the loader object itself is not reachable. Reachability here includes references we rarely think about: a thread's context class loader, a running thread created by the loader's code, a value in a ThreadLocal on another thread, a JDBC driver in DriverManager's list, a static field of a class in a parent loader that holds a reference down into the child, and reflection caches.
- GC rootslive threads, stacks, statics of parent-loaded classes, JNI handles
- Leaked references
ThreadLocalvalues, registries, drivers, context class loaders - Instancesany object of a class defined by the loader
- Class objectseach instance points to its class
- Defining class loadereach class points to its loader
- All classes of that loaderthe loader lists every class it defined
6 layers, from the top down. Step through them one at a time.
Any path from the top to the loader keeps every class of that loader in metaspace.
3When the Collectors Actually Unload
Unloading is part of garbage collection, but not of every collection. A young collection does not unload classes; it never traces the whole object graph. Unloading happens when the collector completes a full trace. In a full GC, every collector unloads classes whose loaders are dead. G1 has unloaded classes at the end of its concurrent marking cycle since JDK 8u40, so a healthy G1 application can unload loaders without ever doing a full GC. ZGC has unloaded classes concurrently since JDK 12, as part of its normal cycles. The behaviour is controlled by -XX:+ClassUnloading, which is on by default; switching it off would make metaspace only grow.
So in a service using G1, a dead loader is usually unloaded after the next concurrent cycle, which G1 starts when the old generation passes its occupancy threshold. A service whose heap is almost empty may go a long time without such a cycle; metaspace growth itself also triggers a GC when it reaches the current high-water mark, called MetaspaceSize at first, which then gives the collector a chance to unload.
4Why the Built-In Loaders Never Unload
The bootstrap loader is part of the JVM itself and is not even a Java object. The platform and application loaders are held by the JVM and by static fields in the JDK, for example ClassLoader.getSystemClassLoader() always returns the same live object. Since those loaders are always reachable, the classes they define stay loaded for the life of the JVM. That is why anything on the class path, our own classes and every library jar, stays in metaspace until the process exits, and why only code loaded by loaders we create, such as plugins, web applications in a container, and scripts, can ever be unloaded.
5Hidden Classes: The Exception
Hidden classes, added in JDK 15, are classes defined with MethodHandles.Lookup.defineHiddenClass. They cannot be found by name through any loader, and the JDK uses them for lambda implementations and other generated code. Unless they are created with the STRONG option, a hidden class is not kept alive by its defining loader, so it can be unloaded as soon as nothing references it, even while its loader lives on.
This is what lets frameworks generate many short-lived classes without leaking metaspace, as long as they do not keep the classes in caches. It is not a general mechanism for ordinary classes.
6Seeing Unloading and Loaders
Three tools cover most investigations. Unified logging shows each class as it is unloaded. jcmd lists every loader with the number of classes it holds and the metaspace they use. And the loaded-class counters show the trend.
The second listing shows the classic sign of a leak: two loaders of the same type with the same number of classes, where only one should exist. From there, a heap dump and a path to GC roots for the extra loader show what keeps it alive.
7Follow-up questions
Why does a single leaked ThreadLocal value keep an entire web application loader alive?
A ThreadLocal value is stored in a map inside the Thread object. Container worker threads belong to the container and live across redeploys, so they are GC roots that outlive the application. The map's key is a weak reference to the ThreadLocal, but the value is a strong reference. If the value is an object of a class from the web application, the path goes thread -> map -> value -> its class -> its loader -> every class of the application, so one small object pins thousands of classes in metaspace. Removing values with ThreadLocal.remove() in a finally block avoids it.
Can a young collection unload classes?
No. Young collections only trace the young generation and treat old objects as roots, so they cannot prove that a loader is unreachable. Unloading needs a complete marking, which is why it happens in full collections and in the concurrent marking cycles of G1 and ZGC.
Does a dead loader free its metaspace immediately?
The metadata is freed when the collector unloads the loader, and the memory goes back to metaspace for reuse. Returning it to the operating system happens later and depends on the JDK version; modern JDKs reclaim unused metaspace more eagerly than older ones did.
8Conclusion
In this answer, we saw that a class can be unloaded only when its defining loader is unreachable, which means no reachable instances, Class objects or references to the loader, including hidden ones such as ThreadLocal values and registries. Unloading happens during full GCs and the concurrent cycles of G1 and ZGC, never in young collections. The bootstrap, platform and application loaders are always reachable, so their classes stay loaded for the life of the JVM, while hidden classes not strongly tied to their loader can be unloaded independently. -Xlog:class+unload and jcmd VM.classloader_stats show what is happening.
Open with
A class can only be unloaded together with its defining class loader, and only when that loader is unreachable. That means no instance of any of its classes, no Class object and no reference to the loader itself can be reachable from GC roots, and that includes things like a thread's context class loader, a ThreadLocal value on a pool thread, a JDBC driver in DriverManager or a static cache in a parent-loaded library.
Then, if they want more
Then a collector has to run a cycle that unloads classes. Young collections do not; a full GC does, G1 does it at the end of concurrent marking, and ZGC does it concurrently in its normal cycles. The class metadata in metaspace is freed then.
Classes from the application loader are effectively never unloaded because the bootstrap, platform and application loaders are referenced by the JVM for its whole life, so they never become unreachable. That is why only code in loaders we create, like plugins or web applications in a container, can ever be unloaded.
The exception is hidden classes, which the JDK uses for lambdas: unless they are defined as strong, they are not kept alive by their loader and can be unloaded on their own.
To investigate I use -Xlog:class+unload to see unloading, jcmd VM.classloader_stats to see each loader with its class count and metaspace, and a heap dump with paths to GC roots for any loader that should be gone.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 12, Java 15
- 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 "When can the JVM unload a class, and why are classes loaded by the application class...": https://scrivio-interview-library.netlify.app/java/class-loading/c520d5fe1c/