• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

GC and the String pool (p419 K&B book)

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm just reading about the String pool on page 419/420 of the K&B book and I have a question.

Do un-referenced elements in the String pool get garbage collected just like any other object?

The reason I ask is that if elements in the String pool get GC'ed shortly after they become unreachable, wouldn't that reduce the efficiency of the String pool?

On the other hand, if the String pool is never flushed of unused Strings, it could get very big indeed.

So in practice, are Strings in the String pool just like any other object from a GC point of view, or do they get some kind of special treatment? (e.g. does the GC only go after the String pool if it has exhausted all other options?)

From what it says on p420 about forcing a new String object using new String("my new string");, I take it that Strings made this way are definitely treated as normal objects, since they are allocated outside of the String pool?

I'd be very grateful if someone could clear that up for me. Thanks.

Andy
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,


As I know the GC, it only applies to Object created using "new" operator and whenever you use "new" object is created on the heap.

So GC is only concerned about heap that is separate from what you call String constant pool. G.collector is never concerned about String Constant Pool.




Regards,
cmbhatt
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings in String pool are garbage collected.

Please see the following Javaworld page
http://www.javaworld.com/javaworld/javaqa/2003-12/01-qa-1212-intern.html
 
Andrew Ebling
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Kan:
Strings in String pool are garbage collected.

Please see the following Javaworld page
http://www.javaworld.com/javaworld/javaqa/2003-12/01-qa-1212-intern.html



OK, well I guess if it is that complicated and JVM specific, we are unlikely to see a question hinge-ing on this subtlety on the exam right?

Thanks for your help!

Andy
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sri Kan,


Sri Kan says,
Strings in String pool are garbage collected.



I would have to disagree with you here!
When you talk about String interning (not on the exam though), it says,
search for the object if it already exists on the heap, make this reference variable refer to the same Object on the heap, otherwise do the normal procedure.


Straight from the K&B page 246:


Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.





Alright?


Regards,
cmbhatt
 
Sri Kan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far nobody told me about a question on this topic in the exam and I did not come across a question on this topic in the exam too. So I guess you can safely assume it will not be asked.

I would have to disagree with you here!
When you talk about String interning (not on the exam though), it says,
search for the object if it already exists on the heap, make this reference variable refer to the same Object on the heap, otherwise do the normal procedure.



Chandra, search on the heap happens and new one is created when not found. One way to test whether a string for which references are removed is the same that is returned later on is to print hash code of the strings with same literal values. Example in the Javaworld article does the same.
 
Andrew Ebling
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Kan:
So far nobody told me about a question on this topic in the exam...



But nobody is allowed to discuss real exam questions with anyone else right? (Remember that legal agreement everyone has to click through?)
 
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic