This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes  is Strings in Java are not garbage-collected ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark " is Strings in Java are not garbage-collected ?" Watch " is Strings in Java are not garbage-collected ?" New topic
Author

is Strings in Java are not garbage-collected ?

Arpit Chaudhary
Greenhorn

Joined: Dec 28, 2007
Posts: 7
Strings are stored in special "String Pool" which is not garbage-collected, ever. So all discarder string will sit there, wasting memory live zombies, never alive and never destroyed. Please tell me it is true.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Arpit: Strings are stored in special "String Pool"

References to strings literals are stored in the constants pool of every class. String objects themselves exist in the heap as any other object.
Arpit:which is not garbage-collected, ever.

Well not completely true. The string constants can be garbage collected if there are no references to them(like any other object). Since on garbage collection of the class definition, the constant pools will also be garbage collected, so, there can be a situation when a string literal is not referenced from anywhere and thus will be gc'ed.

This is an FAQ entry for your reference.


apigee, a better way to API!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: is Strings in Java are not garbage-collected ?