• 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

Garbage Collection and String Literals

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the code

How many objects are elgible for garbage cllection at the end of line 4. I say None.
Can somebody please explain the answer.
Thanks
Sunita
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunita,
Think the following 3 items are available for garbage collection:
str1 - set to 'null' in Line 2. Any object set to 'null' is eligible for garbage collection.
The literal strings "second string" and "third string" as they are no longer being referenced. (Not 100% sure of these, as they would be in the string pool and I'm not clear on gc and the string pool).

------------------
Jane
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think all are String literals...and they should be saved in String pools. There is no new String() created so no String object gc ed?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Ricky

Regards
Khurram
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am bit confused. Why GC is not considering String pool ?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
Please see following URL ---
http://www.javaranch.com/maha/Discussions/Garbage_Collection/Strings_-_Khalid_-_JavaRanch_Big_Moose_Saloon.htm
Since String pool is not in the objectives of SCJP ( as mentioned in the above link) so....
I think there will only be two objects available for GC. i.e. "second string" and "Third string".
Maha Anna please say something about this code.
regards,
Manish
[This message has been edited by Manish Singhal (edited October 08, 2000).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am thinking all string objects str0, str1, str2, str3 will be available for GC, after line 4.
any comments?
Thanks
Santosh Jaiswal
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree with Manish.
also Jane, even if we are setting str1 to null in line 2, the String object that str1 was refering to ( ie. "some string") is still being refered to by str0 now. So that object will not be Garbage Collected.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str0 = null;
String str1 = "some string";
String str2 = "second string";
String str3 = "Third string";
str0 = str1; // 1
str1 = null; //2
str2 = str0; //3
str3 = str2; // 4
According to me, at line 4 , this will be the case:
str1 str0 str2 str3
| \ | / null Some string
therefore, second string and third string will be ready for garbage collection.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your information, string literals( constructed using the = operator instead of new() ) are stored in the String pool. These strings are re-used if the program tries to create identical strings.
Strings in the string pool live longer than the objects created using the new() operator. The JVM garbage collects the strings in the pool when the class is unloaded.
Please note that you are not required to know about GC of string literals for the exam. The GC-related questions in the exam almost always deal with regular new()'d objects. However, if in case questions do appear about GC of string literals, treat them as any regular object.
Hope that helps,
Ajith
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic