• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Alok Das
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Hema
 
zulfiqar raza
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
thanks,
Zulfiqar
 
Satadal Bhattacharjee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also am convinced with Hemalata's answer will Ajith please help us out.coz I am totally confused after seeing both the answers.
Regards,
Shankar.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too feel the correct answer is (e). The explanation given by zulfiqar raza justifies the answer.
Ajith
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graeme,
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.
HTH,
Aruna
[This message has been edited by Aru Ven (edited October 23, 2000).]
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic