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?

Javdroider
  • 136
  • 8
  • 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… – Holger Apr 27 '18 at 06:16
  • @Holger I think it may be some difficulty or side-effect to provide this function. – Javdroider Apr 27 '18 at 07: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? – Holger Apr 27 '18 at 07:17
  • Because Java uses garbage collection. An `unloadClass()` method would be insecure. – user207421 Apr 27 '18 at 07:45

2 Answers2

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?

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
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

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32