Garbage collection of String literals

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:

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

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

  • 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 [eg: 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 [eg: 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 ...)


    Under normal circumstances, string literals and classes are all allocated into the JVM's permanent generation ("PermGen"), and usually won't ever be collected. Strings that are interned (eg mystring.intern() ) are stored in a memory pool owned by the String class in permgen, and it was once the case that aggressive interning could cause a space leak because the string pool itself held a reference to every string, even if no other references existed. Apparently this is no longer true, at least as of JDK 1.6 (see, eg, here).

    For more on permgen, this is a decent overview of the topic. (Note: that link goes to a blog associated with a product. I don't have any association with the blog, the company, or the product, but the blog entry is useful and doesn't have much to do with the product.)


  • The literal string will remain in memory as long as the program is in memory.
  • str will be garbage collected, but the literal it is created from will not.
  • That makes perfect sense, since the string class is unloaded when the program is unloaded.
  • 链接地址: http://www.djcxy.com/p/86702.html

    上一篇: 甚至在dialog.dismiss()上泄漏窗口

    下一篇: String文字的垃圾回收