Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unloading a class from JVM..????  RSS feed

 
Harish Kashyap
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
is it possible to unload a class which is loaded in JVM, I mean dynamic reloading of a class only when its necessary instead of keeping it loaded all the time

thx
harish
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, BUT, the only way to do this is to dispose of the class loader that was used to load the classes.
You have to create a custom class loader, and early in your loading process, use your class loader to load the classes you might wish to later unload. When you dispose of the class loader, all the classes loaded by it are also disposed. Then you can create a new class loader and reload those classes, thus getting the effect of dynamic loading.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:

You have to create a custom class loader, and early in your loading process, use your class loader to load the classes you might wish to later unload. When you dispose of the class loader, all the classes loaded by it are also disposed. Then you can create a new class loader and reload those classes, thus getting the effect of dynamic loading.

Ok i have a doubt. Say i have a custom classloader which loads the first version of a class. As usual i assign the loaded class instance to an interface handle.
Then i try loading the changed version of the class through another Custom Class loader instance.
Now even if i explicitly set the earlier class loader to null, will the first version of the class really unload? since i'm holding to an instance of that class?
My guess is that in that case 2 versions of the class will reside in the JVM.
can anyone confirm this?
thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!