This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why does discarding String pool objects waste memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why does discarding String pool objects waste memory?" Watch "Why does discarding String pool objects waste memory?" New topic
Author

Why does discarding String pool objects waste memory?

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I'm just reading about the StringBuffer and StringBuilder classes in Kathy Sierra & Bert Bates's SCJP book.

I understand that String objects are immutable, and that modifying these Strings will mean that the references to them are lost and so they are discarded and lost in the String pool.

What I don't understand is why this is such a problem. Won't the memory used by these objects be reclaimed by Garbage Collection? Or is the point that Garbage Collection takes resources and so it is better to reuse exactly the same memory without having to reclaim it?

What I'd also like to confirm is that the memory from discarded String objects in the String pool CAN be reclaimed by Garbage Collection and isn't lost forever.

It would be really cool if someone could clarify this point for me.

Many thanks

Joe


OCPJP 85%
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Joe Lemmer wrote:Hi there,
I understand that String objects are immutable, and that modifying these Strings will mean that the references to them are lost and so they are discarded and lost in the String pool.


Modifying string object will create more string objects in the heap and no reference variable that refers to its old string objects(Current refernce variable will refer to newly created string object. i am assuming that you are using the same reference variable). And those objects will be still available in the heap.
Garbage Collection will look for these objects only in heap not in string pool.That is why we go for StringBuilder or StringBuffer.
New String object will not be created if it is already available in string pool.

Regards,
Anbarasu



A.A.Anbarasu
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
read this http://www.javaranch.com/journal/200409/Journal200409.jsp#a1.

String object whose references stored in String constant pool is not eligible for garbage collection. They lives in memory for long intervals, not eligible for garbage collection immediately, that's why we should use StringBuilder. String objects are handled different way for garbage collection than normal objects.


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Joe Lemmer wrote:I understand that String objects are immutable, and that modifying these Strings will mean that the references to them are lost and so they are discarded and lost in the String pool.

I'm not sure what you mean by "discarded and lost in the String pool".

If a string is in the string pool, which is so for string literals in your source code and strings on which you called intern(), it will not be garbage collected, because the string pool is holding a reference to the string object.

String objects are not always in the string pool. A string object that's not in the string pool, and to which your program doesn't hold any references anymore, will be garbage collected.

Strings never get "lost in the string pool".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi,

Sorry it's taken so long to reply, but I'd like to say thank you to everyone that replied. It has been extremely helpful.

I understand now that the String Pool itself holds references to each String that is created, so if we do not use the StringBuilder or StringBuffer classes when doing lots of work with Strings, then it's easy to see that a lot of memory will be taken up.

Thanks again

Joe
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Joe Lemmer wrote:I understand now that the String Pool itself holds references to each String that is created

This is not exactly true. Read the Javaranch Journal again.
Javaranch Journal wrote:When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
 
subject: Why does discarding String pool objects waste memory?