• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collection basics  RSS feed

 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start by telling us where that question comes from.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Sir. It's from Enthuware
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • 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: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is correct
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!