This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Practice exams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Practice exams" Watch "Question from Practice exams" New topic
Author

Question from Practice exams

Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Hi,



The answer is:
At GC point 1 -> 3 objects are eligible for GC.
At GC point 2 -> 0 objects are eligible for GC.

I understand, second anser. But I feel the first answer should be => 2 objects are eligible for GC.

Larsen.



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
Nope. The answer is 3. Why do you think it should be 2?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Larsen,

At GC point 2, there are two object eligible for GC (f1 and f2), so, option 2 is not the answer.

Now, GC point 1 comes after GC point 2, and at GC point 1, another object is eligible for GC(the one created by new Fiji()), so, at GC point 1, three objects are eligible for GC.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
Well, at point 2 there are no object eligible, because the two objects created in the go() method are still reachable. As soon as the method returns though, they become unreachable.

You're correct about point 1 though.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Stephan,

At last statement of method go()

So, both objects created in method go() are now unreachable, and hence eligible for GC, right?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
No, f3 still has a reference to the object that was referenced by f2, and that object's f field references the object that was initially assigned to f1.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Yes Stephan, you are right. I missed it

f3 refers to f1.f, so f1 is not eligible for gc. Further, f1.f refers to f2, so f2 also is not eligible for gc. So... is it that both options are correct (i.e. no object is eligible for gc at GC point 2)?

Need to brush up gc fundamentals again
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Got it. Just ignored the simple fact. Since nothing is returned from the method, all the objects created in the method should be available for GC. Additionally, nothing is persisted in the static field. Thank you guys.


Larsen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Practice exams
 
Similar Threads
Objects eligible for GC
SCJP for Java 6, Garbage Collection question
Exam prep question
Garbage Collection question
K&B ch-3 Ques-2