Wes Biggs wrote:

> On Mon, 10 Apr 2000, Sam Heisz wrote:
> >
> > "JDK 1.1.x specification added the feature of class unloading...
> > where the only reference to the Singleton object is maintained within
> > the Singleton class itself, the garbage collector, in its enthusiasm to
> > dispose of any unused trash, may assume that the Singleton is
> > unreachable because no other object or class holds a current reference
> > to it and could quite legally garbage collect the object and unload the
> > class...'
> >
> > It goes on to mention that you either have to maintain a reference to
> > the Singleton somewhere, or use the -noclassgc flag to disable class
> > unloading.
>
> The spec states "A class may not be unloaded while any instance of it
> is still reachable. A class or interface may not be unloaded while the Class
> object that represents it is still reachable."
>
> The first clause of that seems to imply that a singleton would be saved
> from GC because its class object will never be collected, even if the
> ClassLoader that loaded it in the first place (as in the case of JSPs)
> gets dumped.

It's actually the other way around in at least some JVMs -- the garbage collector
detects no other object references to an instance of your Singleton class (probably
because there is no such instance, if you're using all static methods).  Therefore,
it considers the class not in use, and garbage collects it.  The problem,
especially in 1.1, is that the GCs did not consider the opportunity for you to call
a static method directly as a "reference".  Therefore, people with long running
apps (like servlet engines) started getting really strange "class not found"
exceptions on their singleton classes.

I understand that most JDK 1.2 based GCs are smarter about this.  But, I don't use
Singletons like this so I couldn't tell you this from any personal experience.


>  So effectively you would end up with a special cluster
> (class and singleton instance) that is valid and not subject to garbage
> collection but are not part of the root tree of references -- hence
> Craig's assertion that this is more of a forest than a tree.
>

I said "probably" -- the weasel word is because every JVM implements garbage
collection their own way, so the internal implementations are very likely to be
different.  But every GC implementation is going to have starting point(s) within
the JVM for tracing chains of object instance references.

>
> Wes
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to