A bit of a silly question. I've already looked around and seen that there's no way to force an unload of a class unless you wrote a custom JVM. However, I've been wondering if it was possible to do so by using the Unsafe class and manually empty the address of the class object, thus theoretically "unloading" the class. I do, however, predict there will be issues when an Object with said class is still in memory, therefore either promptly crashing the JVM or defaulting the object's class to Object.class or null.
-
Do you have a use case or is this just a general question? – benjamin.d Nov 23 '13 at 01:51
-
If you have full control over the environment, use a seperate classloader to load that class or JAR and then unload the class loader (probably including a call to `System.gc();`) See http://stackoverflow.com/questions/148681/unloading-classes-in-java – Philip Whitehouse Nov 23 '13 at 01:53
-
I'm currently having issues trying to get classes to unload properly and want a way to forcefully unload said classes. edit: Just saw Philip's answer. I had a feeling I would have to use a custom class loader or some sort. If possible, I would still like for this question to be answered, declaring if it's just plainly impossible or not. – Cirno Nov 23 '13 at 01:56
1 Answers
However, I've been wondering if it was possible to do so by using the Unsafe class and manually empty the address of the class object, thus theoretically "unloading" the class.
It is highly unlikely that you can find all places where the state of a loaded class is referenced. If you don't find them all, the garbage collector won't think it is unreachable and unloading won't be triggered. And if you (somehow) used Unsafe
to smash the state of a (still) reachable class, then the end result is likely to be a hard JVM crash.
I do, however, predict there will be issues when an Object with said class is still in memory, therefore either promptly crashing the JVM or defaulting the object's class to Object.class or null.
The former, most likely.
Is there a way to force unload a class by using the sun.misc.Unsafe class?
AFAIK, No.
In short, don't try to do this. The probability of it "working" is vanishingly small ...
Apparently your motivation is ...
I'm currently having issues trying to get classes to unload properly and want a way to forcefully unload said classes.
What you should be doing is figuring out what is preventing the classes from unloading. The root cause is most likely that you still have references somewhere to some object or class defined by the classloader. Eliminate those references, and the classes should unload.
(Conceptually speaking, there is a hard, bidirectional "reference" between a class and all of its instances, and a hard, bidirectional "reference" between a classloader and all of the classes that it defined. Just one external link to an instance or class, or to the classloader, is sufficient to inhibit gc'ing / unloading for the entire network of objects.)

- 698,415
- 94
- 811
- 1,216