Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Kathy's Book- GC question

 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following Q is from Kathy's book on SCJP1.4

After line 6 runs. how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
Answer given
10. B. By the time line 6 has run, the only object without a reference is the one generated
as a result of line 4. Remember that “Java is pass by value,” so the reference variable x is not
affected by the m1() method.

My answer is C. I completely agree with Kathy's answer and explanation given above. But what about the object created in the method m1()? Isn't that also eligible for GC as no thread can reach it?
I would like to hear from Kathy.
Thanks in advance,
Sarma
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what about the object created in the method m1()? Isn't that also eligible for GC as no thread can reach it?

Sarma,
Infact, thats the only object eligible for GC. According to the explanation, if you closely observe, the only object without a reference is the one generated. This object is generated in the m1() method and its reference is passed as a return reference variable to the line 4. Apart from this object , there is no new object being created at line 4
Hope this helps
Sri
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sri. I missed that.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sri -
You beat us to the punch !
good job..
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert,
Hope I can repeat the same performance in the final exam
Sri
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic