• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection of non-heap strings

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic