It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Alok Das

Joined: Oct 06, 2000
Posts: 2
At what point is the object anObj available for garbage collection.

01: public class Base{
03: private void test() {
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
14: static public void main(String[] a) {
15: new Base().test();
16: }
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
Hemalatha Rajagopal

Joined: Oct 18, 2000
Posts: 10
I believe that the answer should be option c. Bcoz the value of the variable anObj is being changed as null which tells the gc that now it is eligble.

Thanks & Regards,
zulfiqar raza
Ranch Hand

Joined: Oct 18, 2000
Posts: 81
Correct me, I think the answer is e.
As there are two reference variables pointing to the same object string. One of the reference is set to null, but the other reference still points to the original string object. Only after you exit the code block will the references go out of scope and leave the object dangling to be garbage collected.
can someone help here?
Satadal Bhattacharjee

Joined: Oct 21, 2000
Posts: 1
I also think that answer e is correct. c cannot be correct since the reference locObj points to the same object and gc cannot reclaim the memory till ALL references to the object are freed.

[This message has been edited by Satadal Bhattacharjee (edited October 21, 2000).]
shanks iyer
Ranch Hand

Joined: Oct 16, 2000
Posts: 47
I also am convinced with Hemalata's answer will Ajith please help us out.coz I am totally confused after seeing both the answers.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I too feel the correct answer is (e). The explanation given by zulfiqar raza justifies the answer.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
I agree with Zulfiqar's explanation, however I still think the answer should be (c). It depends on your interpretation of the the phrase "the object anObj" in the question.
After line 09 anObj no longer references any object and the new String created at line 08 is therefore available for gc, even though locObj still references the String originally pointed to by anObj.

Mary Anitha

Joined: Oct 13, 2000
Posts: 23
anObj is an object reference which is cannot Garbage collected.
I think what the question means by "the object anObj" is the object referred to by anObj.
Anyway there is only one object here.
So the answer e seems correct
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Am I wrong in thinking that line 8 will generate a new String object?
What happens to that, is it immediately available for gc, or does anObj now reference the new String?
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Line 8 won't create a new String bcoz ..there is nothing to trim in
String anObj = "sample";
Suppose if the above was like this -
String anObj = " sample"; // note the leading spaces
then line 8
08: anObj.trim(); would create a new String.
[This message has been edited by Aru Ven (edited October 23, 2000).]
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Thanks Aru
I also found this in the API documentation
"If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned."
So you are right, there is only one object
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
I think e) is the correct answer, and here's why:
a) is obviously wrong because there are 2 ref's to anObj at that point.
b) is wrong because the locObj reference still points to anObj, and the anObj ref does too (nothing to trim).
c) and d) are both wrong because locObj is still referencing the anObj object - the trim() method doesn't create a new object because there's (still) nothing to trim.
The only answer left is e).
Hope this helps!
I agree. Here's the link:
subject: Garbage Collection
jQuery in Action, 3rd edition