Home > Archive > Java Help > August 2005 > Does GC kill instances of class Class?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Does GC kill instances of class Class?
|
|
| notbob@tessellation.com 2005-08-31, 6:59 pm |
| If a class has no instances, does garbage collection label the
corresponding instance of Class as "garbage" and collect it?
If it does, and that Class has static data, then wouldn't the static
data be lost if the Class instance is collected?
Thanks,
Robert
| |
| Roedy Green 2005-08-31, 9:56 pm |
| On 31 Aug 2005 16:18:56 -0700, notbob@tessellation.com wrote or quoted
:
>If a class has no instances, does garbage collection label the
>corresponding instance of Class as "garbage" and collect it?
This is my best understanding.
classes (the things holding the static members) are objects just like
anything else. They get GCed when all references to them go away,
INCLUDING the references in the ClassLoader.
That particular ClassLoader reference won't go away until the
ClassLoader itself goes away.
And THAT will happen only for user invoked custom ClassLoaders. The
basic system default one will hang in there to the end.
When you have a womb of some sort, all bets are off what will happen
since you have no idea what games they play with ClassLoaders unless
you have studied them carefully.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
| |
| Mike Amling 2005-08-31, 9:56 pm |
| notbob@tessellation.com wrote:
> If a class has no instances, does garbage collection label the
> corresponding instance of Class as "garbage" and collect it?
Not exactly. Instances of Class can be garbage collected, but more is
required than just that the class have no instances. Many classes never
have any instances. The class's ClassLoader has to have become eligible
for garbage collection. Note that Class instances from the system class
loader (which is the only class loader unless you make your own) can
never be garbage collected.
> If it does, and that Class has static data, then wouldn't the static
> data be lost if the Class instance is collected?
That's one reason why they aren't.
--Mike Amling
| |
| Thomas Hawtin 2005-08-31, 9:56 pm |
| Roedy Green wrote:
>
> classes (the things holding the static members) are objects just like
> anything else. They get GCed when all references to them go away,
> INCLUDING the references in the ClassLoader.
>
> That particular ClassLoader reference won't go away until the
> ClassLoader itself goes away.
And if there are any other classes loaded by that class loader, they
will keep it alive. Therefore, all classes loaded by the same class
loader disappear at the same time.
It's easy to write memory leaks that keep class loaders alive.
java.lang.ThreadLocal, java.sql.DriverManager and
java.beans.Introspector all have issues. (I've sent patches, but they
haven't been applied.)
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
|
|
|
|
|