• 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

Can String objects be GCed?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects will be eligible for GC just after the method returns?

public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}

Came across that question and was told that the String "str" wouldn't need to be GCed because it's a string and goes in the string pool. Same with "abc". Is that true? I figure it's still an object so it still is eligible no?

Thanks for any guidance on this
 
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
First, to answer your question "Can String objects be GCed?": Yes, String objects are just like any other kinds of objects and they are garbage collected just like other kinds of objects.

Note that there is only one String object in that piece of code, and that's the String object that represents the string literal "abc". The variable str refers to that String object.

Java has a special optimization for strings: the string pool. For string literals, such as "abc" in your code, a String object is created and placed in a pool. When you use "abc" multiple times in your program, the same String object in the pool is used; Java does not create more than one String object with the content "abc". This saves memory.

So when the variable str goes out of scope, the String object that it points to will not be garbage collected - it will remain in the string pool.

Note that the string pool is normally only used for string literals. Other String objects, that do not come from String literals, are not maintained in the pool, and will be garbage collected normally. (Well, you can explicitly make a String go into the string pool by calling intern() on it, but you would not normally do that in a Java program).
[ December 16, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panseer,

Just to add to it,

if it was instead of
then there are now two objects now=> the "abc" thats placed in the literal pool and the "abc" that resides outside the literal pool(this is created because of the "new").

Now in this case, the second object that is in the non-literal pool is eligible for garbage collection.

Thanks
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is an interesting topic, and for those of you who are focusing your studies to what is on the exam, the good news is that on the real exam, you will never be asked to understand how the GC interacts with objects of type String.

In other words, it's NOT on the exam
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone. Also Bert that good to know. I got that question from Eunthuware exam, kinda strange that they had it if it won't be on the test but definitely still interesting to know.

Gonna examine this a little more in depth.
 
reply
    Bookmark Topic Watch Topic
  • New Topic