7

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.

Community
  • 1
  • 1
  • Was about to answer, but the first link on google was this question on SO: [Unloading classes in java?](http://stackoverflow.com/questions/148681/unloading-classes-in-java) – Andreas Dolk Sep 13 '11 at 11:12
  • Why do you need that? Btw, that is dependent on the JVM and might not be done at all (see the infamous PermGen problem) – Thomas Sep 13 '11 at 11:13

3 Answers3

3

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?)

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 1
    Do the Java classes provided by JDK (e.g. `java.util.ArrayList` and `java.math.BigIntege`r) get unloaded at all? – Pacerier Nov 20 '14 at 08:59
1

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.

Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
  • No, it _might_ then be elligible for garbage collection but in some cases the class might actually never be collected. – Thomas Sep 13 '11 at 11:14
  • Not correct. A class will (may) be unloaded only if it's classloader gets garbage collected. You're answering *objects/instances*. – Andreas Dolk Sep 13 '11 at 11:15
  • Agree , references to the class and to the classloader itself should be removed . – Sandeep Pathak Sep 13 '11 at 11:17
  • @CodeBuzz - there are *no* (Java-)references to the class. The *class* is a jvm internal datastructure, based on the class file. I have the feeling ,that you still mix up classes and instances... – Andreas Dolk Sep 13 '11 at 11:22
  • As per my knowledge when an class loaded in to memory an instance of class Class is created and which content the information about the loaded class. we can get the instance of class Class by writing ClassName.class. So to unload a class from memory , that instance of class Class should be collected by garbage collector. But here is the main point, when the garbage collector destroy the Object of class Class(created due to loading of class) generally an object is eligible for garbage collector , if it's lost all his reference. so when and how the object of class Class lost his all references –  Sep 14 '11 at 10:23
0

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

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133