Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

About Garbage Collection!!

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The String s = "hello"; s = null; is not garbage collected but String s = new String ("hello"); s = null; is garbage collected.
Are these two declarations of String different ? I can't understand it! Could someone here help me?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
This topic has been discussed before. You might want to try searching on word combos like 'new' and 'garbage'. You'll probably get a broader discussion that way. Here is just one of the many discussions: http://www.javaranch.com/ubb/Forum24/HTML/002268.html
Hope that helps,
--Kelley

Originally posted by Tony Sam:
[b]The String s = "hello"; s = null; is not garbage collected but String s = new String ("hello"); s = null; is garbage collected.
Are these two declarations of String different ? I can't understand it! Could someone here help me?[/B]


 
Tony Sam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
furthermore,i saw a question like this:
1)From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
i think it must be D), but the correct ans is A) , why??
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
I believe that is because in line 3, string1 is set to null, so the literal "Test" is not being used anymore and then eligible for garbage collection.
Anyone else??
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tony
I still feel the answer is D)0
String literals are not G'ced
Correct me if i am wrong.
Neha
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha,
Why wouldn't they? Don't string literals reference an instance of the String class? If they're a reference, then why wouldn't they gc'd? I'd like to hear what other people have to say as well. I could be totally wrong?
I'm curious as this is a really good question.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rick
References to String literal object are added to a pool that is not g.c.ed. Search th Ranch for examples.
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks Jose, I'll do that.
reply
    Bookmark Topic Watch Topic
  • New Topic