I have read that string pools are actually part of perm generation. And string pools do not generally get garbage collected, as a reference still exists even after GC runs and references are no longer used. So does it really get garbage collected? And as far as perm gen is concerned, it requires a full GC for the garbage collection. So collection happens or not? Kindly help with the confusion
Asked
Active
Viewed 580 times
0
-
So as per your comments, i may assume, during full GC, string literals are never collected. Then Doesn't it cause memory leak, as once used string literals may no longer be used by the application anymore? – zabit Mar 07 '17 at 13:07
-
As I understand it, as of Java 8, there is no PermGem space. – VGR Mar 07 '17 at 13:09
-
No, it doesn't cause a memory leak. A memory leak would mean that at some point you run out of memory. And yes, Java 8 doesn't have a PermGen, but Metaspace, but that's irrelevant to the question of whether the string pool is collected. – Kayaman Mar 07 '17 at 13:10
-
That is a plausible explanation if Java 8 were in use. What could be the case for versions prior to Java 8 ? – zabit Mar 07 '17 at 13:11
-
1Case for what? String pool doesn't get collected, end of story. – Kayaman Mar 07 '17 at 13:12
-
1That is my assertion basically. If string pools are never garbage collected, every new string would remain in the memory, even though they won't be referred. So it does make memory leak, if spaces are not freed up ? – zabit Mar 07 '17 at 13:15
-
Strings don't go to string pool unless they're literals, or explicitly put there with `intern()`. Since Java 7 non-literal strings (i.e. ones you've explicitly interned) can be removed from the pool if no references exist. There are no memory leaks anywhere. – Kayaman Mar 07 '17 at 13:21
-
Do you understand the difference between a `String` literal and just a `String`? – Kayaman Mar 07 '17 at 13:26
-
As per my knowledge, a string literal is created when we are just referring to a random string (like, String str = "Apple") on the pool, and Strings are created when we use the new operator (like, String str = new String('Mango') ) on the heap. And if intern is used on 'Apple', we could reference the created interned string to 'Apple', thereby maintaining a single reference to that literal. Is there any more that could be added to it ? – zabit Mar 07 '17 at 13:34
-
Then why did you talk about all Strings going to the pool? – Kayaman Mar 07 '17 at 13:46
-
That was a typo. My doubt was that - what happens to all the string literals when they get left in the pool, as those string literals are not garbage collected, as per your comments. So if they stay there, isn't there memory leak ?? – zabit Mar 07 '17 at 17:20
1 Answers
1
In short, JVM String pool is collected during major GC.
String object in pool are subject for normal Java garbage collection rules.
String pool is similar to WeakHashMap in its semantic. Once String object doesn't have any strong reference it would be collected and reference would be removed from pool.
Prior to Java 7, String literal in pool may be collected only if perm gen is included in collection (not every Full/Old GC includes perm gen in scope).
In Java 7 and Java 8 pool itself and pooled string literals are part of normal heap.
You can find more details in this article.

Alexey Ragozin
- 8,081
- 1
- 23
- 23