问题描述
在探索了 java 的字符串内部结构后,我对所谓的perm 空间"感到困惑.我最初对它的理解是它包含 String
literals 以及类元数据,如 这个问题.
After exploring java's string internals I've grown confused on what is referred to as the "perm space." My understanding initially of it was that it held String
literals as well as class meta data as explained in this question.
我还阅读了有关 String.intern()
方法的信息,它会将 String
放入字符串池中,返回对其唯一实例的引用.据我了解,这是保存在 JVM 永久空间中的字符串文字的同一个字符串池.在我看来,烫发空间"似乎不可能是可修改的(毕竟它是永久的,是吗?).但后来我发现 this question 其中 EJP 对已接受的最高投票评论答案说明了
I've also read about the String.intern()
method and that it places String
s into the String Pool returning a reference to unique instance of it. It is my understanding that this is the same string pool holding String literals that exists in the JVM's perm-space. It didn't seem possible to me that the "perm-space" could be modifiable, (it is permanent after all, yes?). But Then I found this question where the top voted comment by EJP on the accepted answer explains that
Intern'd 字符串已经支持 GC 好几年了.
Intern'd strings have been GC-able for quite some years now.
暗示 GC 在 perm-space 上运行,这似乎不是很永久.这如何调和?GC 会检查 perm-space 中的所有内容吗?GC 是否检查字符串池中的所有内容,包括来自源的字符串文字?实习生字符串是否有第二个字符串池?GC 是否只知道在收集时查看实习生字符串?或者这个评论是错误的并且实习一个字符串会阻止它被 GC(我希望不是这种情况)?
Implying that the GC runs on the perm-space which doesn't seem very permanent. How does this reconcile? Does the GC check everything in the perm-space? Does the GC check everything in the string pool including string literals from the source? Is there a second string pool for intern'd strings? Does the GC know only to look over intern'd strings when collecting? Or is this comment mistaken and intern'ing a string prevents it from ever being GC'd (which I hope is not the case)?
推荐答案
字符串字面量 被实习.从 Java 7 开始,HotSpot JVM 将实习字符串在堆中,而不是 permgen.
String literals are interned. As of Java 7, the HotSpot JVM puts interned Strings in the heap, not permgen.
在 java 7 之前,热点将实习字符串放在 permgen 中.但是,permgen 中的实习字符串被垃圾回收了.显然,permgen 中的类对象也是可收集的,所以一切in permgen 是可收集的,尽管在某些旧 JVM 中默认情况下可能不会启用 permgen 收集.
Prior to java 7, hotspot put interned Strings in permgen. However, interned Strings in permgen were garbage collected. Apparently, Class objects in permgen are also collectable, so everything in permgen is collectable, though permgen collection might not be enabled by default in some old JVMs.
被实习的字符串字面量将是声明类对象持有的对实习池中字符串对象的引用.因此,只有当引用它的 Class 对象也被收集时,才会收集实习字面量 String.
String literals, being interned, would be a reference held by the declaring Class object to the String object in the intern pool. So the interned literal String would only be collected if the Class object that referred to it were also collected.
这篇关于实习生字符串、字符串池和 perm-space 上的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!