wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another GC question help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another GC question help" Watch "another GC question help" New topic
Author

another GC question help

wei liu
Ranch Hand

Joined: Nov 19, 2008
Posts: 35

hi guys the quest below is on SCJP book Page 284 question 11;




the question is until the code to do stuff() how many objects are eligable for GC

answer is 1

can some one tell me which is the one object to be GC and how to work it out.

thanks very much.


ps: my understance is there will be only 5 objects created which are a1,b1, a2,b2. and alpha static instance, and the instance field in Alpha "Beta b2" will just inital to null when Alpha object is created. a1.b2 and a2.b2 are just references to object b1 and b2 respectively, therefore when a1 assign to null only object a1 is eligable to GC, am I correct?


SCJP 1.6 in 2010
SCWCD5 in 2011
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Try to make a drawing for yourself with on one side the objects, the other side the references and in the middle arrows to connect the references with the objects. Then walk through the code and change the arrows. Once you're done you'll know which objects are eligible. But remember that Alpha.b2 is static.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

This question has been discussed a lot of times before. See these threads

http://www.coderanch.com/t/481398/Programmer-Certification-SCJP/certification/many-eligible-gc
http://www.coderanch.com/t/474980/Programmer-Certification-SCJP/certification/Garbage-Collection-Clarification
http://www.coderanch.com/t/469606/Programmer-Certification-SCJP/certification/Garbage-Collection


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another GC question help
 
Similar Threads
Objects Eligible for GC
Self Test Answers Chapter 3 Question 11
kathy sierra question doubt
Eligible for garbage collection
Question about object creation