File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question from mock exam 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 "GC question from mock exam" Watch "GC question from mock exam" New topic
Author

GC question from mock exam

Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Hi
from K & B mock exams

At What point is only a single object eligible for GC?

My answer is after line 9 excutes, because the the third constructed object is no more reachable

But the exam answer is "Never in this program", it was explained that 3rd object is still "has-a" ref to another object which is still alive.

I still have a doubt, because it does not make a sense to have a useless object in the heap, even if 3rd object is still "has-a" ref to another alive object, because that ref also is not reachable.

explain to remove my doubt and verify the correctness of the question and its answer, please.

thanks


SCJP 5
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
My answer is at "Line 10".

Line 3: object 1 created and assigned as e1
Line 4: object 2 created and assigned as e2
Line 5: object 3 created and assigned as e3

Line 6. e3.e assigned to object 2
Line 7: e1.e assigned to object 3
Line 8: e2.e assigned to object 1

Line 9: e3 unassinged to object 3, and e3.e unassigned to object 2, but e1.e not yet unassinged to object 3
Line 10:e2 unassinged to object 2, plus the result from Line 9 -> object 2 eligible GC


Hard work rewards
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
oh how I love these questions


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

The answer given is correct. There is no place that a single object created is eligible for gc.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
Originally posted by Mohammed EL-Adawi:
My answer is after line 9 excutes, because the the third constructed object is no more reachable

After line 9, e3 is set to null. However, the objected that was pointed to by e3, is still referenced by e1.e because of line 7.


But the exam answer is "Never in this program", it was explained that 3rd object is still "has-a" ref to another object which is still alive.

Correct. e1.e


I still have a doubt, because it does not make a sense to have a useless object in the heap, even if 3rd object is still "has-a" ref to another alive object, because that ref also is not reachable.

Partially correct. A circularly referenced island of objects that you seem to suggest can indeed be garbage collected. However, in this case, object pointed to by e1 is not eligible for GC (at line 9) because e1 is still in scope and is not set to null (as yet). So the object pointed to by e1 is referenceable and so is e1.e, and so is e1.e.e, and so on.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Sorry i couldn't catch what and why the correct answer is. Could you clarify my questions below? Thanks!

- Were 3 objects created and was each of then referred to 2 variables?

e2.e and e1 -> object 1
e3.e and e2 -> object 2
e1.e and e3 -> object 3

- Was e3.e de-referred to object 2 because of e3=null at line 9?

e2=null at line 10 -> object 2 unreachable -> GC eligible?
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

http://www16.brinkster.com/themuppeteer/gc2.jpg


Left side = the references
right side= the heap


Everything on the right side (the objects on the heap) that isn't connected to the left side (your references) at a certain point is eligible for GC.
Since all objects on the heap are connected to each other, ALL connections to the references must be cut (by assigning them to null)


so...

6. e3.e = e2;//the green arrowes in the picture
7. e1.e = e3;
8. e2.e = e1; // here all objects are connected to each other, and as long as there is still a way to access them (via a reference on the left side) they wont be garbage collected. Note the connection must not be directely, if there is a path to the object via the other objects on the heap then this is also good and the object is not eligible for GC.

9. e3 = null;// the blue crosses
10. e2 = null;
11. e1 = null;
// only here all connections are cut and the island of three objects can never be accessed anymore, so the GC will detect that.
Hope this clarifies...

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Thanks every one

I don't know why I did not notice line 7, well, I hope that I can see it in real exam
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
if i understood now...

To JVM's knowledge these circular interconnected objects are still referrenced to each other thus not eligible to GC although they're unreachable any longer from the application after e1=e2=e3=null.

Thanks!
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

f i understood now...

To JVM's knowledge these circular interconnected objects are still referrenced to each other thus not eligible to GC although they're unreachable any longer from the application after e1=e2=e3=null.

Thanks!


No! From the moment they are unreachable, the garbage collector detects that and cleans up the interconnected objects. Otherwise you would get a memory leak.
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
Thanks, Mark!

So the concrete correct answer to the original question should be something like "None of single object is eligible to GC but all the objects after the code at line 11 being executed."
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

thats right. all three of them die at the same time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question from mock exam