What mechanism causes classes to be unloaded by the JVM?
When a class is referenced (i.e. with a new statement) for the first time, the class definition is loaded using either the default class loader or a custom one if you’ve defined one. The class definition is stored in an area of the Java heap called the Perm Space. All allocated instances of that class definition have a reference back to the reference stored in the Perm Space. When there are no longer any class instances that reference that class definition, the definition becomes a candidate for collection. The next time a full garbage collection takes place, all class definitions in the Perm Space that aren’t referenced by any actual instances will be cleaned up or unloaded. One way to trigger a full garbage collection is if the Perm Space fills up. This can be seen by watching the output of -Xverbosegc and looking for the cause of GC.