Jeff said:
then,does it work like following?
1.[...]
No. The following is a simplification (there are exceptions in certain
"advanced" situation -- all of which involve classloaders), but it's close
enough for a working approximation:
When a class is loaded, the code for its instance methods
and static methods are treated identically. They are copied
into the JVM's memory and stay there (with the class itself)
until the JVM exits. Nothing is ever GCed.
Instance fields are part of the instances, so the memory used
for storing any instance's fields is allocated when the instance
is created, and released when the instance is GCed. Static
fields are part of the class, and their memory is allocated when
the class is loaded, and never released until the JVM exits.
(Or until the class is itself GCed -- but that can never happen
except in special applications which make sophisticated use
of classloaders)
(Actually, the JVM is at liberty to play tricks to reduce memory consumption,
as long as that is never visible to application programmers. That's an
implementation detail, and will vary from JVM to JVM. As far as I, personally,
know the only "trick" that is used is that the JITed form of the methods' code
/can/ be created lazily (when the method is used, or is used more than a
certain number of times), and that the compiled machine-code representation
/can/ be thrown away if it has not been used for a while.)
I'm puzzled by Roedy,static fields can be GCed? And static methods
too?
Roedy is wrong. If he is confusing you then it's probably best just to ignore
him (on this subject).
-- chris