aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Clarification" Watch "Garbage Collection Clarification" New topic
Author

Garbage Collection Clarification

Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Hi I am reviewing for scjp just want to clarify a question I've encountered regarding eligibility of Garbage Collection.

Here's the code:



When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime
Answer:
® ✓ C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.
®˚ A, B, D, E, and F are incorrect based on the above. (Objective 7.4).

My Question is , in this line CardBoard c3 = c1.go(c2); ====> c3 becomes null right? so should also be eligible for GC?

Thank You in Advance!
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
c3 is null but it never referred to an object...


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

CardBoard c3 = c1.go(c2); //here we are not making any object, rather than simply initializing it with null value.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Ow okay got it. Thank You.
Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Hi I have another part of the mock exam to clarify, Hope you dont mind.

Here is the code:



When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5


Answer:
® ✓ B is correct. It should be clear that there is still a reference to the object referred to by
a2, and that there is still a reference to the object referred to by a2.b2. What might be
less clear is that you can still access the other Beta object through the static variable
a2.b1—because it's static.
®˚ A, C, D, E, and F are incorrect based on the above. (Objective 7.4)

My Question: For what I understand
This line of codes creates 4 objects:

---->Beta b1 = new Beta(); Beta b2 = new Beta(); Alpha a1 = new Alpha(); Alpha a2 = new Alpha();

and this line of codes puts a1,b1 and b2 to be eligible for GC

----> a1 = null; b1 = null; b2 = null;

Thank You Again in Advance!
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Hiii
as i could evaluate this code only those objects are eligible for garbage collection which cant be referenced by any code in program.
so as in your question a1=null really makes the obj unreachable but after making b1 and b2 null the objects referenced by them can still be referenced as follows b1 can be referenced by class name Alpha.b1 because its static, and for b2 we will have to look for a2 since a2 is not made null so we can still access b2 with the help of a2.b2


hope this helps

Correct me if i am wrong
Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Thank You for the Reply By the Way,

I Think Im getting it, Correct me if I am wrong, Here's what I think happen.


This line creates 4 Objects on the heap



Then the following line:



And then the following line:



Therefore a1 only is eligible for Garbage Collection because the object created by b1 and b2 are also "Pointed by static Beta and Beta in class Alpha"

Please Correct me if I am wrong.

Thank You for your help Mahesh chitale ,Minhaj kaimkhani and Raju Champaklal !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection Clarification