RE: JavaMemoryModel: When can a classloader be unloaded

From: Evan Ireland (
Date: Wed May 19 2004 - 23:45:13 EDT


"Any potential continuing computation" could in general include one
that uses the reflection API to invoke a static method.

Consider a class:

    class MyCache
        static HashMap myCache = new HashMap();

        static HashMap get()
            return myCache;

A piece of code which calls MyCache.get() in a loop, using the reflection
API (Class.forName(...).getMethod(...).invoke(...)) will break if class
garbage collection is used, as any data that is put in the cache after
one call might disappear by the next call.

Frankly, I think that class GC is a broken concept. We disable it
in our application server because it breaks user-defined caches.

-----Original Message-----
[]On Behalf Of Thomas Hawtin
Sent: Thursday, 20 May 2004 2:42 p.m.
Subject: Re: JavaMemoryModel: When can a classloader be unloaded

Bill Pugh wrote:
> But we couldn't absolutely convince ourselves that running a static method
> of a class ensures the class is reachable.
> (By the way, section 12.7 of the JLS (2nd edition) says "A class or
> interface may
> be unloaded if and only if its defining class loader may be reclaimed
> by the
> garbage collector as discussed in 12.6." But it isn't discussed there.

I assume that comment refers to the reclamation of objects in general,
not just class loaders. In particular (start of 12.6.1) "A reachable
object is any object that can be accessed in any potential continuing
computation from any live thread."

In the case quoted if both the class of class loader can't be reached
(through class constants, the Method, security managers, exceptions, the
thread, etc), as there's no finalizers, the objects are reclaimable. It
would be a bit freaky though.

Tom Hawtin

JavaMemoryModel mailing list -

JavaMemoryModel mailing list -

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