Java remove classes from classloader

how to unload loaded classes

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:

to unload my classes or is it a batter way?

Advertisements

Chris Uppal

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.

Источник

Читайте также:  Python set join to string
Оцените статью