• 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 of Strings

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I came across this question in one of the mock exam. Please explain the answer:
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
Answer given is A. but I think the answer should be D since string 'if not created using new', goes to String pool which is not Garbage Collected! (Am I wrong?)
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the answer on this forum
refer to
http://www.javaranch.com/ubb/Forum24/HTML/000153.html
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I am not so sure about that. Correct me if I am wrong. According to your link, which goes on and talks about the String pool and all, its OK, but I am not sure if I agree with that one!
I agree with the answer being A, where ONE string object is a candidate for Garbage collection.
1) String string1 = "Test";
2) String string2 = "Today";
3) string1 = null;
4) string1 = string2;
On lines 1 and 2, two String objects are being created; then the two string object references, string1 and string2 are being pointed to them, respectively.
When, the first string object reference is set to "null" on line three, the first string object "Test" is a candidate for garbage collection. Then on line 4, the nullified string object referenced is now pointing to the string object "Today". It does not change anything, but still keep the candadacy of the first object for garbage collection.
Rohit.
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding:
Answer 1 would have been correct if the Strings were created like this:
String s1 = new String("Test");
then, the a new String object would have been created on heap and eligible for GC. Since s1 is created without 'new', this will be created in String pool, which is a special case which are not eligible for garbage collection.
Any suggestions?
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI!
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
I think answer a is correct beacause on line 3 the object created on line 1 is referenced to null. And on line4 a new object is created beacause strings are immutable.Each we try to modify the object a new object is created with the change.Hope that is clear.
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right, correct me if this understanding is wrong:
Its not the "test" that is eligible for GC but object string1.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there you're wrong sandeep!
it's "test" that's eligible... there is no such thing as object s1! s1 is a reference! and that to the object "test"....
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets compare with the question posted on previous thread:
http://www.javaranch.com/ubb/Forum24/HTML/000153.html
String s = "abc";
String s1 = "def";
s=null;
How many objects are eligible for GC.
- Here is answer:
a. "none"
b. 1
c. 1 if I added s=s1 as the last line.
I am little confused (more now) ;(
Help me guys!
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a String object is put to the constant pool, it stays there till the JVM exits (an optimization for object re-use). This having been said, consider the following piece of code:
String s = "Hello";
s = null;
String t = "Hello";
For variable t to be set to refer to the still-alive "Hello" object there must be a "hidden" reference somewhere, kept by the JVM; otherwise there would be no way to refer to "Hello".
That is the reason constant pool objects don't get GC'd. The garbage collector considers them (and rightfully so) non-garbage and does not collect them. Your answer should be 0.
Hope this helps,
Panagiotis.
 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be sure:
Answer to my original question:
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
is 0?
If I replace first 2 lines of codes with:
String string1 = new("Test");
String string2 = new("Today");
Answer would be 1?
Please correct me if I am wrong.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sandeep ,,
According 2 me...The Ans. will be 0 ...no Objects r eligible for
GC.
But..in the 2nd case I think if u write:
String string1 = new String ("Test");
String string2 = new String ("Today");
The Ans. should be 1 this time.
What I am trying 2 say tha u r right.
the ans. will be 0 and 1 resp.
I think it will help u.
Ratul Banerjee

Originally posted by Sandeep Chaturvedi:
Just to be sure:
Answer to my original question:
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
is 0?
If I replace first 2 lines of codes with:
String string1 = new("Test");
String string2 = new("Today");
Answer would be 1?
Please correct me if I am wrong.


reply
    Bookmark Topic Watch Topic
  • New Topic