This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection of Strings

 
Sandeep Chaturvedi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I found the answer on this forum
refer to
http://www.javaranch.com/ubb/Forum24/HTML/000153.html
 
Rohit Sahasrabudhe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rat Ban
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic