Re: JavaMemoryModel: Class Loaders and Garbage Collection

From: Thomas Hawtin (
Date: Sat Jun 07 2003 - 20:11:14 EDT wrote:
>>Are you asking about section 12.7 in the JLS, or did you have something
>>else in mind?

2.17.8 of the JVM spec (2nd Ed).

>>I'm not aware of anything specific about class loader reachability.
> Yes...
> We have a vague recollection that there was a rule that:
> A) if an instance of a class is reachable, then the class loader is
> reachable, and
> B) if the class loader is reachable, then any class object it loaded is
> reachable as well.

"A class or interface may be unloaded if and only if its class loader is
unreachable. The bootstrap class loader is always reachable; as a
result, system classes may never be unloaded."

Also, Class has a reference to its ClassLoader.

The quoted statement is not true of early implementations of Java. You
could have ended up with static initialisation running more than once
and Class hash codes changing.

> However, I am pretty sure this rule doesn't make any sense. For example,
> if these were the only rules, then we could collect the class loader as
> soon as we load it, before it loads any classes.

Normally something would keep a reference to the class loader class in
order to do anything useful with it. ClassLoader instances and their
Classes fall together. So if you want a Class to be promptly
collectable, make it with a small scope ClassLoader instance.

Tom Hawtin

Unemployed Java Developer of Central London.

------------------------------- JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:46 EDT