• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection of non-heap strings

 
Harish Verma
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a string were created without using the new keyword as follows:

String s = "abcd";

This string "abcd" will be placed in the special Java string pool. My question is whether this pool is subject to garbage collection or not. If it is, is there any difference in the way garbage is collected from standard heap and this special pool? I think this pool is not a target for collection, can someone pls confirm this? Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, let me clear up a common misconception: the "String pool" is not a special area of memory where Strings are allocated; it's basically a hash table where references to Strings are kept. The Strings themselves, like all other Strings, are in the heap.

Now, will such Strings ever be garbage collected? The JLS is silent on this question. I've been told that in some implementations, they can be, but only if the class that defined them itself collected. This will only happen if the ClassLoader that loaded the class is collected; and this happens only with user-created class loaders, so it's not something you'll come across in a beginner situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic