views:

74

answers:

1

Is a "static final" directly allocated into young gen or old gen or perm gen? (I guess it will most likely land into old gen over the time I suppose.) If it is allocated in the perm gen then, will it be garbage collected when class unloading takes place in Perm Gen ?

+3  A: 

Is a "static final" directly allocated into young gen or old gen or perm gen?

AFAIK, an object referenced by a static final variable will be allocated according to the same rules as any other object. It is most likely to be allocated in the young generation, or in the old generation (if it is large and certain other conditions apply).

[The reason I think this is that the object will be allocated by new executing in some arbitrary code. The JVM is probably not in a position to know that the object will (eventually) be assigned to a static final variable.]

The space for the frame containing the static variables is probably allocated in permGen. Of course, this is not a regular Java object.

If it is allocated in the perm gen then, will it be garbage collected when class unloading takes place in Perm Gen?

That depends on whether the permGen is garbage collected. In modern JVMs it is, and I would expect that the objects referenced by an unloaded classes statics would be garbage collected in the same GC cycle, or the next one ... assuming they were unreachable.

Either way, you should not code your application to depend on any of these details. They are JVM specific.

Stephen C
+1 "Either way, you should not code your application to depend on any of these details. They are JVM specific." Static final, means that it is static and final. Seems dangerous to assume more...
bwawok