3

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 ?

trincot
  • 317,000
  • 35
  • 244
  • 286
Ashish
  • 2,521
  • 2
  • 21
  • 21
  • Unless you're running in debug mode, you cannot unload a class, thus a static final declaration never gets garbage collected by the JVM until the application ends. – Yanick Rochon Sep 27 '10 at 00:36
  • 1
    @Yanick - actually, any class that was loaded using `Class.forName()` can potentially become unreachable, and subject to unloading / garbage collection ... depending on the JVM options. – Stephen C Sep 27 '10 at 01:44
  • 1
    @Stephen C, true. However this is assuming that the class is, in fact, a candidate for GC and the JVM is setup to allow it. In most cases, it won't happen. But I guess I'm going off topic now... – Yanick Rochon Sep 27 '10 at 02:26
  • @Yanick - your comment says "never" ... and that's not correct. – Stephen C Sep 27 '10 at 02:56

1 Answers1

8

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

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 object will be allocated by new executing in some arbitrary code. The JVM is 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.


Note that as of Java 8, this questionis moot since permgen is no more.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 3
    +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 Sep 27 '10 at 03:27