• 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 basics

 
Ranch Hand
Posts: 47
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per the following question when will be the object is eligible for the GC?

Question :Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?



I think it's line 5 but as per some website it is line 6 .

Please correct me if i am missing something as i think when obj will assigned null, from that point it is eligible for GC as i far as i remember i have read so in Java book by K&B.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start by telling us where that question comes from.
 
Linkon Manwani
Ranch Hand
Posts: 47
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Sir. It's from Enthuware
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe either of the lines marked 5 or 6 are correct. Enthuware said it should be line 6? That's odd. There is a forum where you can talk about possible incorrect answers.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why is the object not eligible for garbage collection after line 9 of the code? The line ending // line 4, that is.
You can answer that if you draw a diagram with obj1 and obj2 to represent the two Objects created. You draw arrows from any references to the objects in question. Then you look at the reference o in the tc instance. Then you can work out why I was mistaken earlier. And also why those websites (please tell us which) say the first object is only eligible after line 11 marked // line 6.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I don't believe either of the lines marked 5 or 6 are correct. . . .

That is what I thought at first. But I think I was mistaken. I think line 6 (i.e. line 11) is the correct answer.
 
Ranch Hand
Posts: 99
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This would be my guess but I'm not entirely sure now ...

//line 1: Object obj created
//line 2: still there
//line 3: the Object referenced by obj is now also being referenced by the private field o in the NewClass instance tc, so there are 2 references to the same "original" Object
//line 4: obj now references a new Object, but the "original" one still has a reference in the NewClass instance tc in its private field o
//line 5: obj is now null, but the "original" one still has a reference in the NewClass instance tc in its private field o
//line 6: now o in tc is assigned to obj (which is null), so the "original" Object is not being referred to anymore and becomes eligible for garbage collection


 
Linkon Manwani
Ranch Hand
Posts: 47
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can answer that if you draw a diagram with obj1 and obj2 to represent the two Objects created. You draw arrows from any references to the objects in question. Then you look at the reference o in the tc instance.



Thanks a lot that worked. In line 11[line 6] o in tc also referenced to null where as it is not in line 10 [line 5].
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is correct
 
Did you just should on me? You should read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic