When does a class unload from heap memory [duplicate]
Possible Duplicate:
Unloading classes in java?
When does a class unload from memory?
For loading a class we can call Class.forName("NameOfClass");
or when we create an object of a class, then the class loaded into memory.
Classes
Classes will be loaded by a classloader and will (may) be unloaded when that classloader is garbage collected. In normal applications, where we don't care about classloaders: classes will never be unloaded.
Instances of classes / objects
Objects will be created on the heap and deleted when the garbage collector detects, that there is no reference to that instance/object anymore.
(Just in short, better details: see question Unloading classes in Java?)
In simple words :
Class gets unloaded when all the references to the class(and their instances) are removed and the classloader used is garbage collected.
When exactly a class will be unloaded is not defined, just as when exactly an object may be garbage collected is not defined. What is defined is when it can be unloaded:
A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector
精彩评论