内部字符串,字符串池和perm-space上的垃圾回收 [英] Garbage collection on intern'd strings, String Pool, and perm-space

查看:157
本文介绍了内部字符串,字符串池和perm-space上的垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索了java的字符串内部之后,我对被称为perm空间的东西感到困惑。我最初的理解是它保存了 String 文字以及类元数据,如这个问题



我也读过关于 String.intern()方法,它将 String s放入字符串池中,并返回对其唯一实例的引用。这是我的理解,这是相同的字符串池,其中包含JVM的perm-space中存在的字符串文字。对我来说,烫发空间可能是可以修改的(它毕竟是永久的,是吗?)。但是,后来我发现了这个问题,EJP就这个问题发表了评论。答案解释说:


国际化字符串已经可以使用多年。


blockquote>

暗示GC运行在似乎不是永久性的perm-space上。这如何协调? GC是否检查了烫发区域中的所有内容? GC是否检查字符串池中的所有内容,包括来自源的字符串文字?内部字符串是否有第二个字符串池? GC在收集时是否只知道查看内部字符串?或者这个评论是错误的,并且实际上是一个字符串阻止它从GC'd(我希望不是这种情况)?

解决方案

字符串文字 arened 。从 Java 7 开始,HotSpot JVM提供了实现字符串在堆中,而不是permgen。



在java 7之前,热点将字符串放在permgen中。但是, permgen中的interned字符串是垃圾收集。显然, Permgen中的类对象也是可收集的,所以一切在permgen中是可收集的,尽管在一些旧的JVM中默认情况下permgen集合可能不会被启用。

字符串字面值被声明为Class对象到intern池中的String对象。因此,只有在引用它的Class对象也被收集的情况下,才会收集字符串字符串。


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.

I've also read about the String.intern() method and that it places Strings 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 strings have been GC-able for quite some years now.

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)?

解决方案

String literals are interned. As of Java 7, the HotSpot JVM puts interned Strings in the heap, not 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.

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上的垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆