Hello,
Your objection makes sense, but here is sometrinh from the JVM specification
from SUN:
"2.17.8 Unloading of Classes and Interfaces 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."
Thought, the term "unreachable" seems confusing.
Lachezar Dobrev wrote:
> Hmmm...
> Oh yes, it does.
> When there are no more instances of a specific class, the class itself is
> being disposed off. Creating new instance reloads the class.
>
> > You see, your scenario couldn't exactly happen:
> > The garbage collector cleans all class instances (in this case only one
> > instance), but not the class itself. The class loader does not unload the
> class
> > bytecode, and the static fields do not reside in any of the class
> instances that
> > are released by the garbage collector. So the static fields shouldn't be
> lost
> > after garbage colection of the class instances.
> >
> > Lachezar Dobrev wrote:
> >
> > > Hi.
> > > This MAY be totally wrong, but I have seen that in a different place
> in a
> > > different problem, in a different need. I got the same thing.
> > >
> > > Problem: Static varialbles are being reset from time to time.
> > >
> > > My Answer: The lifetime of the Servlets/EJBs/JSPs is defined by how
> the
> > > webcontainer/ejbcontainer handles requests and persistance. My GUESS is
> that
> > > this problem is as follows:
> > >
> > > 1. We load a class (Say MYClass)
> > > 2. We use MYClass
> > > 3. We free of use all objects of type MYClass.
> > > --- Some time passes.
> > > --- The Garbage Collector sees this class is not used anymore
> and...
> > > FREES IT!
> > > 4. We try to use MYClass. It doesn't exist, so it is loaded.
> > > --- On loading the static fields are reset.
> > >
> > > Now... Having in mind, that the Servlets/JSPs can be randomly freed
> by
> > > the container (Orion) this situation is quite real. Especialy if we use
> that
> > > Servlet/JSP not-so-often.
> > >
> > > My Resolution: Use a Singleton Class to keep all the needed Static
> > > information:
> > >
> > > /** @todo Modify this class to suit your needs */
> > >
> > > public class StaticHolder{
> > > private int Data = 0;
> > >
> > > private static StaticHolder theCopy = new StaticHolder();
> > >
> > > private StaticHolder(){}
> > >
> > > public static StaticHolder getCopy() { return theCopy; }
> > >
> > > public void incData() { Data++; }
> > > public int getData() { return Data; }
> > >
> > > }
> > >
> > > /** @usage */
> > > StaticHolder.getCopy().incData();
> > > int count = Staticholder.getCopy().getData();
> > >
> > > Singleton classes are not disposed by the garbage collector (there is
> > > always one reference to the object from the object itself).
> > >
> > > Another way is to define an instance of your servlet, that will hold
> a
> > > copy of itself :).
> > >
> > > public class MyServlet .......{
> > >
> > > private static MyServlet CopyHolder = new MyServlet();
> > >
> > > }
> > >
> > > This will keep your class always loaded.
> > >
> > > Hope this helps.
> > > Lachezar
> > >
> > > > Sorry, But I do not understand what you mean.
> > > > What I want is to have only a counter for all the requests.
> > > >
> > > >
> > > > At 13:35 24/07/2001 -0500, you wrote:
> > > > >Instead of static I would use a member attribute, since for servlets
> > > > >only one instance exists for a given class.
> > > > >
> > > > >Ismael wrote:
> > > > >
> > > > > > I am making some loading tests, in order to test the loading tool
> I
> > > > > > have written some special jsp to deal verify the number of times a
> jsp
> > > > > > has been called.
> > > > > > My jsp is :
> > > > > >
> > > > > > <%@page contentType="text/html"%>
> > > > > > <html>
> > > > > > <head><title>JSP Page</title></head>
> > > > > > <body>
> > > > > > <%!
> > > > > > static int client=0;
> > > > > > static String attribute_name="client";
> > > > > > static Object lock=new Object();
> > > > > > %>
> > > > > > <%-- This JSP will be used with 4 other jsp to test the
> correctness of
> > > > > > the stress tool
> > > > > > --%>
> > > > > > <%
> > > > > > synchronized (application) {
> > > > > > session.setAttribute(attribute_name,new Integer(++client));
> > > > > > System.out.println("Entering client "+client);
> > > > > >
> > > > > >
> > > > > > }
> > > > > > %>
> > > > > > </body>
> > > > > > </html>:
> > > > > >
> > > > > >
> > > > > > It is very simple but the ouput I get is
> > > > > > Entering client 1
> > > > > > Entering client 2
> > > > > > Entering client 3
> > > > > > Entering client 4
> > > > > > Entering client 1
> > > > > > Entering client 1
> > > > > > Entering client 1
> > > > > > Entering client 1
> > > > > > Entering client 1
> > > > > > Entering client 5
> > > > > > Entering client 2
> > > > > > Entering client 3
> > > > > > Entering client 4
> > > > > > Entering client 5
> > > > > > Entering client 6
> > > > > > Entering client 7
> > > > > > Entering client 8
> > > > > > Entering client 9
> > > > > > Entering client 10
> > > > > > Entering client 11
> > > > > > Entering client 12
> > > > > > Entering client 13
> > > > > > Entering client 14
> > > > > > Entering client 15
> > > > > > Entering client 16
> > > > > > ..
> > > > > > That is, my static variable it is being ignored. I am using Orion
> > > > > > 1.4.5 , Windows 2000 and J2SE 1.3.0_02.
> > > > > >
> > > > > > Anybody know why this could be happening ?
> > > > > >
> > > > > > Regards,
> > > > > > Ismael
> > > > > >
> > > > > >
> > > >
> > > >
> >
> >