• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection Question

 
ManChun Lam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is taken from Sierra & Bates SCJP for Java 5, Chapter 3, Self-test Q2



Q: When // do Stuff is reached, how many objects are eligible for GC
A: C. 2

I got the same answer but my explanation is different.

First, the book's version: C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

My explanation: c3 == null, c1 == null. Therefore the answer is 2.

Am I missing something here?
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My explanation: c3 == null, c1 == null. Therefore the answer is 2.


Well, the above explanation implies that there are 2 Cardboard objects + 2 Short objects in each of the Cardboard objects eligible for GC.
But that is incorrect.

c3 is a reference and was never assigned any object as the go() method returns a null.

So the correct answer is *c1* and the enclosed object *story* are eligible for GC.
 
ManChun Lam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

c3 is a reference and was never assigned any object as the go() method returns a null.


Thank you very much for this explanation. I completely overlooked this.
 
Henry Wong
author
Marshal
Pie
Posts: 21435
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question comes up very often. Please search the forums for other discussions about this... There is also an Errata related to this question.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic