Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Can String objects be GCed?  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 8977
17
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!