How does garbage collection work on static variables? I think since the static variables don't have any references pointing to them , but always lives in memory once the class is loaded, garbage collection is not possible on static variables. Is that true? Any thoughts on this?
I think that variables in general are normally allocated on the stack, not the heap, so they are not available for garbage collection. (When they go out of scope, they simply go away, like when a method returns). Now for the objects they point to- they are on the heap and subject to garbage collection.
So the static variables themselves, I don't know where they are, but I would suspect they are on the stack, in the "main stack frame" area which only goes out of scope when the program terminates, but I don't know this for a fact I am guessing.
Now the objects that a static reference points to could be in the heap, and available for garbage collection when that reference points to something new.
Thanks Tom. That helps. Just to reiterate what you mentioned, the objects that the static references point to can be garbage collected , but the static references themselves do not get garbage collected until after the program terminates.
So the static variables themselves, I don't know where they are, but I would suspect they are on the stack, in the "main stack frame" area which only goes out of scope when the program terminates, but I don't know this for a fact I am guessing
I believe that local variables are on the stack but static and instance variables are parts of objects kept in the heap.
Mike Gershman
SCJP 1.4, SCWCD in process
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking