String文字的垃圾回收 [英] Garbage collection of String literals

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

问题描述

我正在阅读有关垃圾收集的内容,当我搜索字符串文字垃圾收集时,我收到了令人困惑的搜索结果。



我需要澄清以下几点:


  1. 如果在编译时将字符串定义为literal [例如: String str =java> String str = new String(c $ c>>]然后它会被垃圾回收?

  2. java)。intern()]那么它会被垃圾收集?
  3. 有些地方提到文字只会在时被垃圾收集,字符串类将被卸载?这是否有意义,因为我不认为 String 类会被卸载。


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


可能不是。代码对象将包含对表示文字的 String 对象的一个​​或多个引用。因此,只要代码对象可以访问, String 对象就是。



对象变得无法访问,但仅当它们是动态加载的时候...而且它们的类加载器被销毁。


如果我使用intern方法[例如: String str = new String(java)。intern()]那么它会被垃圾收集?

intern 调用返回的对象将是表示java字符串文字。 (java>文字是在类加载时实现的,当你在你的实习中新创建的 String 对象代码片段,它将查找并返回以前实现的java字符串。)



然而,与字符串文字不一致一旦它们变得无法访问,就可以垃圾收集。 PermGen空间是在所有最近的HotSpot JVM上收集的垃圾。 (在Java 8之前...完全抛弃了PermGen)。


它也将在第1点与字符串字面值区别对待。


否...因为它与字符串字面值相同。

事实上,一旦你明白了发生了什么,很明显字符串文字也没有被特别处理。它只是可达性规则的一个应用...

lockquote

有些地方提到文字只会在 String class将被卸载?是否合理,因为我认为 String 类将不会被卸载。


你说得对。这没有意义。说这些来源是不正确的。 (如果您发布了一个URL,以便我们可以阅读他们对自己说的话,这将会很有帮助...)


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

I need clarification on following points:

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

  2. 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.

  3. 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.

解决方案

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

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.

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

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

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

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 ...

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.

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

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

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