Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

Garbage Collection

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers:

I have a question regarding garbage collection. This question is on ExamLab's Diagnostic Exam and I don't know why the actual answer is given.

How many objects are eligible for garbage collection after executing c.aob = null; ?



Options are 0,1,2,3,4,5 and 'Cannot determine'. Correct answer is 2 and the explanation is 'Draw a diagram'. I have made my diagram and I still think just 1 object is eligible for GC. Can anyone please explain to me why 2 objects are eligible for GC?
 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luis Centeno wrote:
Correct answer is 2 and the explanation is 'Draw a diagram'. I have made my diagram and I still think just 1 object is eligible for GC. Can anyone please explain to me why 2 objects are eligible for GC?



It would help if you describe us what you drew. We can't tell you where you went wrong if you don't tell what your did.

However, I am willing to bet that you missed the object that became eligible at line 10.

Henry
 
Luis Centeno
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for answering, below is the diagram I did...(this is probably the worst diagram ever made, but I made it using paint ) this is how I understand gc. I cannot see how an object becomes eligible at line 10. As far as I understand, only object 3 (blue marked) becomes eligible. Could you please explain what did I do wrong?



Thanks in advance...
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your reference for d is not correct.
Actually after
A d=new A().aob=new A();

the d.aob is null.
The statement can be understood as being equivalent to:


Now in our actual program the object temp is implicit, it has no reference so it becomes eligible for GC after the line 10 executes.
 
Luis Centeno
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sunny Narula...

It seems like I was missing the d assignment since I didn't see that d references the same object as new A().aob. Thank you so much, I get it now...If I am not wrong, the final diagram would be like this...



This makes eligible for gc to objects 3 and 4, I think...

Please let me know if I am right...

Regards...
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The only problem here is this above piece in the code. I also got confused about it. Well Say the last object is created and the bit representing it is stored in new A().aob and those bits are transfered to d So d refers to new A().
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Good Example. I 'got' them. Thanks to All........
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Luis. I was under the impression that one object is eligible for GC. I think the key is resolving the Right hand side first and then assigning the reference to that. Since nothing is pointing to new A().aob, from the stack, the object becomes unreachable and hence ready for GC. This is a good question.

cheers.
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt.

Let have the same code but with one small modification. I have removed the c.aob=null.



Will two objects be eligible for GC in this case. Remember c.aob is still pointing to a.aob.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes still 2 objects will be eligible for gc, as
c=b;//Here c will point to same object as b,
and hence object which was pointed by c prior to above line will be eligible for gc.
and as you said,Object pointed by c.aob will not be available for gc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic