aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection of Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection of Strings" Watch "Garbage Collection of Strings" New topic
Author

Garbage Collection of Strings

Sandeep Chaturvedi
Greenhorn

Joined: Jan 19, 2001
Posts: 22
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

Joined: Jan 19, 2001
Posts: 22
I found the answer on this forum
refer to
http://www.javaranch.com/ubb/Forum24/HTML/000153.html
Rohit Sahasrabudhe
Greenhorn

Joined: Jan 16, 2001
Posts: 4
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

Joined: Jan 19, 2001
Posts: 22
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

Joined: Nov 12, 2000
Posts: 183
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

Joined: Jan 19, 2001
Posts: 22
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

Joined: Jan 19, 2001
Posts: 144
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

Joined: Jan 19, 2001
Posts: 22
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

Joined: Nov 27, 2000
Posts: 233
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

Joined: Jan 19, 2001
Posts: 22
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

Joined: Jan 20, 2001
Posts: 30
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection of Strings