• 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

Why does discarding String pool objects waste memory?

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic