-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.2.0
-
generic
-
solaris_2.5.1
Seems to me that java GC should never unload a class that contains
non-final static fields. If the value of a static field is modified,
then we expect that value to hold for the rest of the session. If the
class is unloaded and then re-loaded, the static field will be
reintialized and modification will be lost.
I think it could only happen when classes are loaded indirectly.
Here's a possible scenario:
Suppose there is a class:
class BulletinBoard {
boolean m_killroyWasHere;
}
Suppose no loaded class has code or fields that refer to
BulletinBoard.
Suppose there's another class:
class Kilroy {
public static void main (String argv[]) {
if (!BulletinBoard.m_killroyWasHere)
{
// do something really big
BulletinBoard.m_killroyWasHere = true;
}
}
}
Suppose you load Killroy from a third clas by calling
Class.forName("Killroy"), or using a class loader or something indirect
like that. Note that loading Killroy will cause BulletinBoard to be loaded.
Killroy's main then returns, and the class Killroy is eventually GC'd.
What's to stop BulletinBoard from being GC'd? By hypothesis, no loaded class refers to it (now that Killroy is gone). Now suppose you load Killroty again
by calling Class.forName("Killroy"). Killroy's main will cause BulletinBoard
to be loaded again, and it will see BulletinBoard.m_killroyWasHere == false -- which is incorrect.
non-final static fields. If the value of a static field is modified,
then we expect that value to hold for the rest of the session. If the
class is unloaded and then re-loaded, the static field will be
reintialized and modification will be lost.
I think it could only happen when classes are loaded indirectly.
Here's a possible scenario:
Suppose there is a class:
class BulletinBoard {
boolean m_killroyWasHere;
}
Suppose no loaded class has code or fields that refer to
BulletinBoard.
Suppose there's another class:
class Kilroy {
public static void main (String argv[]) {
if (!BulletinBoard.m_killroyWasHere)
{
// do something really big
BulletinBoard.m_killroyWasHere = true;
}
}
}
Suppose you load Killroy from a third clas by calling
Class.forName("Killroy"), or using a class loader or something indirect
like that. Note that loading Killroy will cause BulletinBoard to be loaded.
Killroy's main then returns, and the class Killroy is eventually GC'd.
What's to stop BulletinBoard from being GC'd? By hypothesis, no loaded class refers to it (now that Killroy is gone). Now suppose you load Killroty again
by calling Class.forName("Killroy"). Killroy's main will cause BulletinBoard
to be loaded again, and it will see BulletinBoard.m_killroyWasHere == false -- which is incorrect.
- duplicates
-
JDK-4037145 System classes lose static state on class unloading.
- Closed
- relates to
-
JDK-4036713 peer objects not visible to GC; classes may be unloaded
- Closed
-
JDK-4065027 Change in class loading/unloading behavior between 1.0.2 and 1.1.3
- Closed