Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Can you unload a single class in Java, for example a plugin class you no longer need? What has to happen before the JVM can free it?

No. Java has no API to unload a single class; classes are unloaded only together with the class loader that defined them. Before the JVM can free a plugin class, its loader, every class that loader defined, their Class objects and all their instances must become unreachable, and then a garbage collection that supports class unloading must run, after which the class metadata in metaspace is released.

Why this question is asked

Anyone building plugins, scripting support or hot-reload features eventually asks how to get rid of classes they no longer need. The answer shapes the whole design, because it decides how code must be grouped into loaders from the very beginning. Interviewers ask it to check that we know classes are not ordinary garbage-collected objects. In this answer, we will recap where classes live, explain why unloading works per loader, list the conditions the JVM needs, see it working in a small program, and look at what this means for designing unloadable plugins.

1A Quick Recap: Where Classes Live

When the JVM loads a class, it creates two things. One is the class's metadata, such as its methods, bytecode, field layout and constant pool, stored in metaspace, a native memory area outside the Java heap. The other is a java.lang.Class object on the heap, which our code uses for reflection and which links to the metadata.

Every class also remembers its defining class loader, the loader that called defineClass for it, and every loader keeps a list of all the classes it has defined. So a class keeps its loader reachable, and the loader keeps all its classes reachable. This mutual reference is the reason classes cannot be removed one at a time.

2Why There Is No Way to Unload One Class

Suppose we could unload just com.acme.plugins.Greeter and leave its loader alive. The loader's cache, the one findLoadedClass consults, still records that Greeter is loaded, and other classes from the same loader may have resolved references to Greeter's methods and fields. Removing Greeter alone would leave dangling references that the JVM would have to hunt down everywhere. Instead, the Java Language Specification says a class may be unloaded only if its defining loader may be reclaimed by the garbage collector. The loader and all its classes are a single unit of unloading, so grouping is our job: if we want to unload something, we must load it in a loader of its own.

3The Conditions Before the JVM Can Free It

Everything in the group must be unreachable from the program's GC roots, the live threads, their stacks, static fields of other loaded classes, JNI references and so on.

Step throughCan the plugin's classes be freed?
  1. Any instance of a plugin class still reachable?
    YesStays loadedNoContinue
  2. Any Class object of the plugin still reachable?
    YesStays loadedNoContinue
  3. Is the loader itself still reachable?
    YesStays loadedNoLoader and classes are garbage
  4. A GC that unloads classes runsfull GC, or a concurrent cycle in G1 and ZGC
  5. Metaspace for those classes is freedchunks returned for reuse

5 steps in order. Play them, or step through them one at a time.

Notice the instances: one Greeter object kept in a cache of the host keeps its Class object alive, which keeps the loader alive, which keeps every class of that plugin alive. The same goes for a reference to the loader itself, for example held in a field of the host, and for a thread created by plugin code, since a running thread is a GC root and its stack and its context class loader point into the plugin.

4Watching It Happen

We can watch unloading with the JVM's unified logging. The program below loads a plugin class in its own loader, uses it, drops every reference, and asks for a GC.

Java
// run with: java -Xlog:class+unload=info Main
URLClassLoader loader = new URLClassLoader(new URL[] { pluginJar }, Main.class.getClassLoader());
Class<?> type = loader.loadClass("com.acme.plugins.Greeter");
Object greeter = type.getDeclaredConstructor().newInstance();
System.out.println(greeter); // com.acme.plugins.Greeter@1b6d3586
loader.close(); // release the jar file
greeter = null; type = null; loader = null; // drop every reference
System.gc(); // a hint; in practice a full GC with G1
// [info][class,unload] unloading class com.acme.plugins.Greeter 0x0000000801001234

The log line confirms the class was unloaded. If we leave one reference in place, for example by keeping greeter, nothing is printed, however many collections run. System.gc() is only a hint and can be disabled with -XX:+DisableExplicitGC, so in real systems we rely on the normal GC cycle, which will unload when it runs a cycle that includes class unloading.

5What This Means for Design

Two consequences follow. First, if plugins must be unloadable, each one needs its own class loader, and the host must interact with plugin objects only through an API defined in the host's own loader, so that it can drop the plugin objects without dropping any shared types. Second, classes loaded by the application class loader, our normal class path, are never unloaded, because that loader lives as long as the JVM. The same is true for the bootstrap and platform loaders. A plugin accidentally placed on the class path can therefore never be removed, however carefully we clean up.

There is one narrow exception: hidden classes, created with MethodHandles.Lookup.defineHiddenClass and used by the JDK for lambdas and similar generated code, can be unloaded on their own when they are not defined with the option that ties them strongly to their loader. That is a tool for framework authors generating code, not a way to unload ordinary classes.

6Follow-up questions

Why is the freed memory in metaspace, not the heap?

Because class metadata, the bytecode, method tables and constant pools, is stored in native memory called metaspace, allocated per class loader. When a loader is unloaded, its metaspace chunks are released for reuse and, over time, can be returned to the operating system. The Class objects and plugin instances are on the heap and are freed by the normal collection.

Can JVM HotSwap be used to remove a class?

No. HotSwap redefines the body of a class that stays loaded; it cannot remove a class or change its structure in a standard JVM. Getting rid of a class always means getting rid of its loader.

Does class unloading happen automatically?

Yes, it is on by default (the -XX:+ClassUnloading flag) and the collector does it as part of its cycles when loaders become unreachable. We only need to make sure nothing keeps them reachable.

7Conclusion

In this answer, we saw that Java cannot unload a single class because a class and its defining loader reference each other and form one unit of unloading. Before the JVM can free a plugin class, the loader, all its classes, their Class objects and all their instances must be unreachable, and then a GC that supports class unloading must run, after which the metaspace is freed. So unloadable code needs its own class loader, while classes on the application class path stay loaded until the JVM exits.

What to say in the interviewAbout 90 seconds aloud

Open with

No, there is no API to unload one class. In Java a class is unloaded only together with the class loader that defined it, because each class references its loader and each loader keeps track of every class it defined, so they form one unit.

Then, if they want more

For the JVM to free a plugin class, the loader itself, every class it defined, their Class objects and every instance of those classes must all become unreachable from GC roots. One plugin object left in a host cache, a thread the plugin started, or a field holding the loader is enough to keep the whole group alive.

Once everything is unreachable, a garbage collection that supports class unloading has to run, which is a full GC or a concurrent cycle in G1 or ZGC, and then the class metadata in metaspace is released. You can see it with -Xlog:class+unload.

The practical consequence is that if I want plugins to be unloadable, each plugin gets its own class loader, the host talks to it only through an interface loaded by the host, and on removal I close the loader and drop every reference.

Classes on the normal application class path are never unloaded, because the application loader lives as long as the JVM, so a plugin accidentally placed there can never be removed.

About this answer

Last updated
12 November 2026
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 "Can you unload a single class in Java, for example a plugin class you no longer need?...": https://scrivio-interview-library.netlify.app/java/class-loading/c28eac1648/