The answer to 'unloading classes in java' says -
"The only way that a Class can be unloaded is if the Classloader used is garbage collected." I took a look at the JLS but couldn't understand it
Why is this the case?
Class unloading is a desirable optimization feature for any Java Virtual Machine implementation. Similar to normal garbage collection, class unloading is the process of freeing memory occupied by various loaded classes that are no longer referenced by the current program execution context.
This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed.
A class is only unloaded when it is garbage collected, and for that to happen there must be no references to it anywhere. And the classloader keeps a reference to each class it loads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With