Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection of String literals

Tags:

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.

like image 239
Lokesh Avatar asked Mar 10 '13 15:03

Lokesh


People also ask

Are string literals garbage collected Java?

So, the object associated with the string literal can only get garbage collected, when the class itself gets garbage collected. This is only possible when its defining class loader and in turn, all other classes defined by this loader are unreachable too.

Does string pool gets garbage collected?

From Java 7 onwards, the Java String Pool is stored in the Heap space, which is garbage collected by the JVM.

Are string literals stored in heap?

The stack will store the value of the int literal and references of String and Demo objects. The value of any object will be stored in the heap, and all the String literals go in the pool inside the heap: The variables created on the stack are deallocated as soon as the thread completes execution.

Where are string literals stored?

The characters of a literal string are stored in order at contiguous memory locations. An escape sequence (such as \\ or \") within a string literal counts as a single character.


1 Answers

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

like image 148
Stephen C Avatar answered Oct 29 '22 10:10

Stephen C