“静态最终”在哪里?直接分配到?年轻一代、老一代还是烫发一代?
“静态最终”是否直接分配到年轻一代或老一代或永久一代? (我想随着时间的推移,它很可能会进入旧代。)如果它被分配在 Perm gen 中,那么当在 Perm Gen 中进行类卸载时,它会被垃圾收集吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由
static Final
变量引用的对象将根据与任何其他对象相同的规则进行分配。它最有可能分配在年轻代或老年代(如果它很大并且某些其他条件适用)。该对象将由在某些任意代码中执行的 new 来分配。 JVM 无法知道该对象将(最终)被分配给静态最终变量。
包含静态变量的帧的空间可能是在 permGen 中分配的。当然,这不是一个常规的 Java 对象。
这取决于 permGen 是否被垃圾回收。在现代 JVM 中确实如此,我希望卸载的类静态引用的对象将在同一个 GC 周期或下一个 GC 周期中被垃圾收集......假设它们无法访问。
无论哪种方式,您都不应该对应用程序进行编码以依赖于任何这些细节。它们是 JVM 特定的。
请注意,从 Java 8 开始,这个问题已经没有实际意义了,因为 permgen 不再存在。
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 astatic 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.
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.