Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can a class not be unloaded without unloading the classloader?

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?

like image 549
Everyone Avatar asked Mar 31 '10 07:03

Everyone


People also ask

What is class unloading?

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.

What is Urlclassloader in Java?

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.


1 Answers

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.

like image 83
Nick Moore Avatar answered Oct 12 '22 22:10

Nick Moore