how to unload loaded classes

Discussion in 'Java' started by Petterson Mikael, Jun 14, 2005.

  1. 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
    Petterson Mikael, Jun 14, 2005
    #1
    1. Advertising

  2. Petterson Mikael

    Chris Uppal Guest

    Petterson Mikael wrote:

    > 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
    Chris Uppal, Jun 14, 2005
    #2
    1. Advertising

  3. Chris Uppal wrote:
    > Petterson Mikael wrote:
    >
    >
    >>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.


    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
    Petterson Mikael, Jun 14, 2005
    #3
  4. 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

    "Petterson Mikael" <> schrieb im Newsbeitrag
    news:d8mn26$b7f$...
    > Chris Uppal wrote:
    >> Petterson Mikael wrote:
    >>
    >>
    >>>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.

    >
    > 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, Jun 15, 2005
    #4
  5. Petterson Mikael

    Chris Uppal Guest

    Petterson Mikael wrote:

    > 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
    Chris Uppal, Jun 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,309
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. James-D-Bloom

    Editing loaded (running) Java classes

    James-D-Bloom, Sep 1, 2003, in forum: Java
    Replies:
    0
    Views:
    404
    James-D-Bloom
    Sep 1, 2003
  3. Udo Corban
    Replies:
    0
    Views:
    3,827
    Udo Corban
    Jan 23, 2004
  4. Stefan Siegl
    Replies:
    1
    Views:
    762
  5. unload classes?

    , Jul 29, 2006, in forum: Java
    Replies:
    3
    Views:
    1,970
    Ian Shef
    Aug 7, 2006
Loading...

Share This Page