• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC question from mock exam

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh how I love these questions
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given is correct. There is no place that a single object created is eligible for gc.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats right. all three of them die at the same time.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic