posted 18 years ago
The byte-code for the methods, the static variables and the constants in a class only have to be loaded once. So once a class is loaded onto the heap the JVM can use that stuff all day long. There are some conditions under which this stuff is unloaded, but I wouldn't count on it.
When you instantiate an object the JVM allocates the member variables. These go in the heap as well. Objects are created and garbage collected in different orders which can leave holes in the heap. The GC can move things around to close up the holes and make the remaining contiguous chunk bigger.
When you call a method and as the method executes, the JVM allocates the local variables. These go on the stack. Method calls exit in reverse order of how they were called, so the JVM can de-allocate in reverse order. It doesn't get holes in the stack.
Does that make sense?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi