4

As menthioned in this post, classes will be uploaded when their ClassLoader is garbage-collected, and OSGI use this to realize hot-depoly.
But why not JDK provide a unloadClass(String name) method to help us remove a sepecify class? Since they have provide loadClass(String name)
Is there any difficulty or side-effect to provide this function?

4
  • 3
    The existence of a loadClass method does not imply that there should be an unloadClass method. Java also has a new operator but no delete operator. Maybe, you recognize a pattern behind it… Commented Apr 27, 2018 at 6:16
  • @Holger I think it may be some difficulty or side-effect to provide this function. Commented Apr 27, 2018 at 7:12
  • 2
    Well, if there was support to unload a class while the class loader is still reachable, the next attempt to load that class implied potential side effects of the class’ initializer. Further, there is no guaranty that the initializer will evaluate to the same class state as before. But far more important, what should happen when you call unload(classXYZ) and there are still instances of classXYZ? Or subclasses? Commented Apr 27, 2018 at 7:17
  • Because Java uses garbage collection. An unloadClass() method would be insecure. Commented Apr 27, 2018 at 7:45

2 Answers 2

1

But why not JDK provide a unloadClass(String name) method to help us remove a sepecify class?

This question can be answered taking into mind one of the core philosophy of java. Java was made keeping in mind that the user does not have to deal with memory management. When you say unloading of class, it essentially means that you removing the references to the class are freeing up memory from the Method area/Class area where the data related to class are stored.

Is there any difficulty or side-effect to provide this function?

The most vital side effect is that the user could interfere with method area which is shared accross all the threads and itself is not always thread safe.

For more in detail understanding of jvm you can refer the link below:

http://blog.jamesdbloom.com/JVMInternals.html

Sign up to request clarification or add additional context in comments.

Comments

1

But why not JDK provide a unloadClass(String name) method to help us remove a sepecify class? Since they have provide loadClass(String name)

Loading a class is a very common action that will almost always need to be performed (either explicitly or in the background) after a classloader is created. Unloading a class is a very specialised action that would only ever see a benefit in a tiny fraction of use cases, and comes with complications and difficulties.

Is there any difficulty or side-effect to provide this function?

Absolutely, it's similar to just randomly deleting an object. If you've loaded the class then it may well be used, and instances of it may be being used within your program. What do you expect to happen in this case when the class is unloaded?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.