Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects eligible for garbage collection

 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Objects eligible for garbage collection after Line 1: 3(ans given)

But according to me,it should be 1.
As a is assigning value to b then c<=b then d<=c.
I know that my logic might not be right.

Please correct me.

Thanks
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the assignments are made from right to left, it means after :
d=c=b=a;
all the object references are now refering to the first created object.
It means the three others created objects are no longer reacheable by
a runnable thread (this is the condition to be eligible for garbage
collection).
Setting the d reference to null doesn't change this fact : at line 1,
the three other objects instance are eligible for garbage collection.

Hope this helps

Gilles
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get you.
Can you please elaborate for me please.
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what is the point you don't understand ? (be more accurate, because
"i don't understand" is not a good starting point for a more detailed
answer).

Gilles
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
-------------------------------------------------------------------

public class Test{
public static void main(String[] args) {
Object a = new Object(); // the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null; } } //Line 1
-------------------------------------------------------------------------

Ok let me explain it..for understanding take
Object a =new Object(); as a new Object is created Of type Object and a Object reference variable a is assigned to it.(say this object as ObjectA).Similary for
Object b = new Object();
Object c = new Object();
Object d = new Object();
three Object will be created(say ObjectB,ObjectC and ObjectD ) and will be assigned some Object references.

d=b=c=a means all four references will now point to ObjectA(initially referenced by reference a ). It means we dont have any reference to access ObjectB,ObjectC and ObjectD. So they are eligible for garbage collection.

Finally
d=null makes variable d to point nothing. So total three Object are eligible for garbage collections.

If i m wrong,please experts correct me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic