• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC question from mock exam

 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh how I love these questions
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer given is correct. There is no place that a single object created is eligible for gc.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3710
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats right. all three of them die at the same time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic