ClassLoader, ambiguity between classes

R

RS

Hello

I use a customised ClassLoader which can be called during runtime to load
all .class file present in a directory and its subs. Problem : when it reads
the classes in a folder which has allready been loaded. In this case,
several classes are loaded twice and I've this strange behaviour

Class c = this.defineClass(null,buffer,0,buffer.length);
print c.hashCode() --> gives
18602441
print Class.forName(c.getName()).hashCode() --> gives 3912376

is it possible to "unload" a class ?

The thing is that I've no way to know the binary name of the class I'm
loading, so I cannot test if class has allready been loaded before... Any
idea ?
 
R

Robert Klemme

I use a customised ClassLoader which can be called during runtime to load
all .class file present in a directory and its subs.

Why did you create a custom class loader for this? Also, this pattern
might not fit well with the usual approach, i.e. classes are loaded when
needed - not earlier. Especially your order of loading is likely
different from the order that would be imposed by class dependencies.
> Problem : when it reads
the classes in a folder which has allready been loaded. In this case,
several classes are loaded twice and I've this strange behaviour

Class c = this.defineClass(null,buffer,0,buffer.length);
print c.hashCode() --> gives
18602441
print Class.forName(c.getName()).hashCode() --> gives 3912376

is it possible to "unload" a class ?

I believe the JVM's GC will collect classes at some point in time (if
they are unused of course). But I doubt you can explicitly unload
classes because that would likely cause loose ends.
The thing is that I've no way to know the binary name of the class I'm
loading, so I cannot test if class has allready been loaded before... Any
idea ?

Can't you use some bytecode reading package like gnu.bytecode to inspect
the class file you are about to load?

My general recommendation is this: do not mess with class loading unless
you have to (i.e. write a servlet container or a JVM, have to fetch
classes from other sources than file system and jars). It is not easy
to get right and I cannot think of a standard application that would
actually benefit from this. My 0.02EUR...

Kind regards

robert
 
C

Chris Uppal

RS said:
I use a customised ClassLoader which can be called during runtime to load
all .class file present in a directory and its subs.

For a start, that's an odd thing to do with a ClassLoader. It's not /wrong/,
but it is not the normal way that classloaders work (they load classes lazily,
and on demand). That suggests that you might have some misunderstanding of the
classloader architecture; what are you trying to achieve ?

Problem : when it
reads the classes in a folder which has allready been loaded.

It should definitely not be trying to load any class twice. Unless you are
doing something strange (and probably wrong) the classloader architecture will
prevent that happening (a classloader is never asked to load a class which the
JVM already knows about).

In this
case, several classes are loaded twice and I've this strange behaviour

Class c = this.defineClass(null,buffer,0,buffer.length);
print c.hashCode() --> gives
18602441
print Class.forName(c.getName()).hashCode() --> gives 3912376

That sounds as if the class file has been loaded "normally" by Java (using it's
own classloaders which scan the classpath), and also by your classloader
(breaking the normal conventions). It is certainly possible to have the same
classfile loaded more than once by different classloaders, and that is sometime
exactly what you want to have happen, but I don't think that's what you want or
expect here ?

is it possible to "unload" a class ?

Only if all the following conditions apply:

The class is eligible for GC itself (no reachable instances,
no reachable Class object, etc).
The classloader which loaded it is eligible for GC.
All the other classes loaded via that classloader are
eligible for GC.

Those conditions /can/ be met, but it takes special design to make it happen --
it's not something that will happen by itself.

-- chris
 
D

Daniel Pitts

RS said:
Hello

I use a customised ClassLoader which can be called during runtime to load
all .class file present in a directory and its subs. Problem : when it reads
the classes in a folder which has allready been loaded. In this case,
several classes are loaded twice and I've this strange behaviour

Class c = this.defineClass(null,buffer,0,buffer.length);
print c.hashCode() --> gives
18602441
print Class.forName(c.getName()).hashCode() --> gives 3912376

is it possible to "unload" a class ?

The thing is that I've no way to know the binary name of the class I'm
loading, so I cannot test if class has allready been loaded before... Any
idea ?

Only the JVM can decide to unload a class, there is no way to force it,
so the answer is basically, no.
However, a class definition is "namespaced" by its name AND the
classloader that loaded it. Perhaps if you tell us more about the
underlying problem you're trying to solve, we help you find a more
elegant solution.

Good luck,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top