Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question 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 "Garbage Collection question" Watch "Garbage Collection question" New topic
Author

Garbage Collection question

Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27


Which are true? (Choose all that apply.)
A. At line 8, one object is eligible for garbage collection.
B. At line 8, two objects are eligible for garbage collection.
C. At line 8, three objects are eligible for garbage collection.
D. At line 18, 0 objects are eligible for garbage collection.
E. At line 18, two objects are eligible for garbage collection.
F. At line 18, three objects are eligible for garbage collection.

The right answers are C, D. Anyone can explain me the steps in detail? Thanks a lot
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39


Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
Henry Wong wrote:
Please QuoteYourSources


The question comes from OCP Java SE 6 Programmer Practice Exams Page 181
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.
Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
Sebanti Sanyal wrote:Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.


Thank your for the explanation it is very clear.

If I remove base=null, what will be the result? I'm a little confused because base is static. Thanks
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58


If I remove base=null, what will be the result? I'm a little confused because base is static. Thanks


Answer will be A and D. The objects created inside go() can still be referenced through base at line 8.
Selena Klasnja
Greenhorn

Joined: Jan 19, 2012
Posts: 1

Wu Wen wrote:
Sebanti Sanyal wrote:Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.


I think it is a good explanation but only if f3 doesn't play any further role. For example:
What would be the scenario if we instead of doStuff at line 18 have something like this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question
 
Similar Threads
Garbage Collector Question-1
K&S - Practice Exam 3 - Q28
Question 10 capitulo 3 kathy sierra book
method returns what happens to garbage collection
Exam prep question