11

According to the famous book Head first Java Page 661:

"Garbage Collector doesn't go inside String pool."

After reading about the similar questions on SO, i have found mixed answers like:

  1. Garbage collection of String literals is same as normal objects. Read this
  2. Some answers say the opposite. Read answer here.

My Questions are:

  1. How were the string literals garbage collected in Java 6 and before ?

  2. And since in Java 7 , string literals will be created on heap, how the garbage collection of string literals will be different in Java 7 as compared to the java 6?

Community
  • 1
  • 1
Aman Arora
  • 1,232
  • 1
  • 10
  • 26
  • Your question is not about Java 6/7/anything, but about one specific implementation of Java (HotSpot). Additionally, it is not about string literals, but about *interned strings*. – Marko Topolnik Dec 05 '13 at 14:04
  • Aren't string literals interned? – Aman Arora Dec 05 '13 at 14:04
  • Yes, they are, but they aren't treated any differently from any other interned strings---except that they have much less chance of ever becoming unreachable. – Marko Topolnik Dec 05 '13 at 14:05
  • ' any differently from any other interned strings' --> Meaning I can make any string interned by calling its intern method. But to make another dynamic string to reference that I need to call its interned method (java wont look up and automatically set the ref). Interned is just 'common' place to keep Strings. All literal strings are interned automatically. All interned strings are not literals. – tgkprog Dec 05 '13 at 14:07
  • I din't say they are treated differently, All i asked is "How are they garbage collected?" – Aman Arora Dec 05 '13 at 14:07
  • Aman, Marko is trying to explain the scope and correction to your answer. If you understand what he is saying will help get the full picture. – tgkprog Dec 05 '13 at 14:09
  • That means they are normal string objects and will be garbage collected when they are not referenced by anyone ? – Aman Arora Dec 05 '13 at 14:13
  • @AmanArora There is an easy answer for "How are string literals garbage collected?" It is "not." – Ingo Dec 05 '13 at 15:17
  • Any reference will make it clear ! Thanks. – Aman Arora Dec 05 '13 at 16:09

1 Answers1

6

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.

Picked up from : (Source).

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289