> >
> > >I have a static counter attribute, and I do not want to see it gc'ed.
> > >Am I missing something ?
> > >
> > >Jean-Luc
> >
> > If it's static it won't be GC'd.
> >
>
> [ SNIP ... ]
>
> For this reason, I don't think using a static member variable as a persistent
> counter makes sense by itself.  While it will survive multiple requests,
> it won't survive class reloading.  Class reloading creates a new instance of
> that static member variable.  If you disable class gc, the old instance
> will be floating garbage, hence the memory leak.
>
> [ SNIP ... ]
>
Class "reloading" is a confusing concept, it definitely confused me for
a while.

Java does not really define class reloading in the way you might
imagine.  Using ClassLoaders, you can load _new_ classes, but you cannot
replace the definition of an old class.  All old linkages, including the
bindings between old instances and their class object, stay exactly as
they were.  If a class refers to another class by name, for example if
it does "new OtherClass()", the code in the first class gets bound once
and for all to an implementation of OtherClass, and class "reloading"
never changes this binding.

On the other hand, code that uses a variable of type Class (say,
"theClass"), and does something like "theClass.newInstance()", can use a
newly-loaded class.  Class loaders can give you a pointer to a new class
object, and that is what JServ's class loader does.

About static members:

Turning off class GC has nothing to do with making things like static
counters work.  GC will only happen if the class becomes unreachable.
And being unreachable means (by definition) you have no way to access
the counter, so you can't check its value.

If you want to make a static counter that never resets even when you
redefine classes, then put it in a class reachable through your
CLASSPATH, and it will only reset when you start a new VM.  If you want
it to continue to count up even when you restart the VM, put it in a
file or a database.

Hope this helps.

-Cris




----------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Archives and Other:  <http://java.apache.org/main/mail.html/>
Problems?:           [EMAIL PROTECTED]

Reply via email to