字符串文字的垃圾收集 [英] Garbage collection of String literals

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

问题描述

我正在阅读有关垃圾收集的信息,但在搜索字符串文字垃圾收集时,搜索结果令人困惑.

I am reading about Garbage collection and i am getting confusing search results when i search for String literal garbage collections.

我需要澄清以下几点:

  1. 如果一个字符串在编译时被定义为文字[例如:String str = "java"] 那么它会被垃圾收集吗?

  1. If a string is defined as literal at compile time [e.g: String str = "java"] then will it be garbage collected?

如果使用 intern 方法 [e.g: String str = new String("java").intern()] 那么它会被垃圾回收吗?在第 1 点中,它的处理方式与字符串文字是否也不同.

If use intern method [e.g: String str = new String("java").intern()] then will it be garbage collected? Also will it be treated differently from String literal in point 1.

有些地方提到只有当 String 类被卸载时,文字才会被垃圾收集?这是否有意义,因为我认为 String 类永远不会被卸载.

Some places it is mentioned that literals will be garbage collected only when String class will be unloaded? Does it make sense because I don't think String class will ever be unloaded.

推荐答案

如果一个字符串在编译时被定义为文字[例如:String str = "java";] 那么它会被垃圾收集吗?

If a string is defined as literal at compile time [e.g: String str = "java";] then will it be garbage collected?

可能不是.代码对象将包含一个或多个对表示文字的 String 对象的引用.所以只要代码对象可达,String 对象就会到达.

Probably not. The code objects will contain one or more references to the String objects that represent the literals. So as long as the code objects are reachable, the String objects will be to.

代码对象可能无法访问,但前提是它们是动态加载的……并且它们的类加载器被销毁.

It is possible for code objects to become unreachable, but only if they were dynamically loaded ... and their classloader is destroyed.

如果我使用 intern 方法 [e.g: String str = new String("java").intern()] 那么它会被垃圾回收吗?

If I use the intern method [e.g: String str = new String("java").intern()] then will it be garbage collected?

intern 调用返回的对象将与表示 "java" 字符串文字的对象相同.("java" 文字在类加载时被实习.当你在你的代码片段中实习新构造的 String 对象时,它会查找并返回之前实习的 <代码>"java" 字符串.)

The object returned by the intern call will be the same object that represents the "java" string literal. (The "java" literal is interned at class loading time. When you then intern the newly constructed String object in your code snippet, it will lookup and return the previously interned "java" string.)

然而,与字符串文字不相同的实习字符串一旦变得不可访问,就会被垃圾回收.PermGen 空间 在所有最近的 HotSpot JVM 上被垃圾收集.(在 Java 8 之前......完全放弃了永久代.)

However, interned strings that are not identical with string literals can be garbage collected once they become unreachable. The PermGen space is garbage collected on all recent HotSpot JVMs. (Prior to Java 8 ... which drops PermGen entirely.)

它也会与第 1 点中的字符串文字区别对待.

Also will it be treated differently from string literal in point 1.

不...因为它与字符串文字是同一个对象.

No ... because it is the same object as the string literal.

事实上,一旦你理解了发生了什么,很明显字符串文字也没有被特殊对待.这只是可达性"规则的应用......

And indeed, once you understand what is going on, it is clear that string literals are not treated specially either. It is just an application of the "reachability" rule ...

有些地方提到只有当 String 类被卸载时,文字才会被垃圾收集?这是否有意义,因为我认为 String 类永远不会被卸载.

Some places it is mentioned that literals will be garbage collected only when String class will be unloaded? Does it make sense because I don't think the String class will ever be unloaded.

你说得对.这没有意义.说那是不正确的消息来源.(如果你发布一个 URL 会很有帮助,这样我们就可以自己阅读他们在说什么......)

You are right. It doesn't make sense. The sources that said that are incorrect. (It would be helpful if you posted a URL so that we can read what they are saying for ourselves ...)

这篇关于字符串文字的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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