Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC

 
sheri
Ranch Hand
Posts: 43
  • 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
please help:
thanx
sherin
 
jag_nai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is
b) After line 8
Some how I feel that this makes the object reference eligible for garbage collection though when it will happen is not possible to note.

[This message has been edited by jag_nai (edited September 19, 2000).]
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
object referred by anobj becomes eligible for garbage collection after line #11.
Because, trim() method will create and return another string object (which will immediately become eligible for GC). Even though anObj is set to null at line # 9, locObj is still referring to it. So the object will become eligible for GC only at the end of its declaring context (in this case the if block).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic