Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

how to unload loaded classes

Thread view: 
Petterson Mikael - 14 Jun 2005 07:22 GMT
Hi,

I have my classes loaded with my own ClassLoader. Now I want to load my
simulated classes ( same package and name). First I have to unload the
previous classes. Shall I use:

System.gc();System.gc();

to unload my classes or is it a batter way?

Cheers,

//mikael
Chris Uppal - 14 Jun 2005 09:13 GMT
> I have my classes loaded with my own ClassLoader. Now I want to load my
> simulated classes ( same package and name). First I have to unload the
> previous classes.

Classes are unloaded at the discretion of the JVM, subject to (at least) the
following restrictions:

   - they will not be unloaded while any instances are still in
       existence (have not been GC-ed).

   - they will not be unloaded while their owning classloader is still in
       instances is still in existence (has not been GC-ed).

   - they will not be unloaded while their java.lang.Class object is still
       referenced from anywhere (same goes for reflective access to their
       members).

So all you can do is ensure that the above conditions are met, and trust that
the classes will be unloaded.  You may find that asking the JVM to do a GC will
help, but I can't see much point myself -- after all, if the above conditions
are met, then you don't /care/ whether they've actually been unloaded.

Note that even if they haven't been unloaded, you can still load new versions
of them in a different classloader.

Basically, the unit of reloading is not the class but the classloader.

   -- chris
Petterson Mikael - 14 Jun 2005 14:45 GMT
>>I have my classes loaded with my own ClassLoader. Now I want to load my
>>simulated classes ( same package and name). First I have to unload the
[quoted text clipped - 15 lines]
> So all you can do is ensure that the above conditions are met, and trust that
> the classes will be unloaded.

I only load my classes ( don't create an instance) and do reflection on
them. Does this mean that they will be grabage collected after my
reflection is done?

You may find that asking the JVM to do a GC will
> help, but I can't see much point myself -- after all, if the above conditions
> are met, then you don't /care/ whether they've actually been unloaded.
>
> Note that even if they haven't been unloaded, you can still load new versions
> of them in a different classloader.

So you mean I create a new instance of my ClassLoader then I can load my
my simulated classes?

> Basically, the unit of reloading is not the class but the classloader.
>
>     -- chris

Thanks for the information.I greatly appreciate it!

cheers,

//mikael
Christian Schlichtherle - 15 Jun 2005 01:14 GMT
A class definition is actually a tuple of its full package name AND the
class loader which loaded it.

This means you can have multiple versions of classes with the same name as
long as they have been loaded by different class loaders.

Have a look at the class URLClassLoader and Thread. The latter exposes a
method to set the current/active class loader. Sorry can't remember the
method name, but I'm sure you will find it in the Javadoc.

Regards,
Christian

>>>I have my classes loaded with my own ClassLoader. Now I want to load my
>>>simulated classes ( same package and name). First I have to unload the
[quoted text clipped - 46 lines]
>
> //mikael
Chris Uppal - 15 Jun 2005 09:10 GMT
> I only load my classes ( don't create an instance) and do reflection on
> them. Does this mean that they will be grabage collected after my
> reflection is done?

Only if you also release all references to the classloader that loaded them
(and to any other classes loaded via the same classloader).   As I said, the
unit of unloading is the classloader, not the class.

> > Note that even if they haven't been unloaded, you can still load new
> > versions of them in a different classloader.
>
> So you mean I create a new instance of my ClassLoader then I can load my
> my simulated classes?

Yup.

BTW, Christian (in a different post in this thread) mentioned
java.lang.Thread.setContextClassLoader().  As far as I know, that's not
relevant for your purposes here.  Indeed, I've never found out what purposes it
is /supposed/ to be relevant to -- the documentation is non-existent and
(again, AFAIK) that data isn't even used in the places where you'd hope that it
would be, such as de-serializing objects of classes that are only known to
custom classloaders.  If anyone has more information/pointers about the
"context" classloader then I'd like to see it.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.