Two qestions about gb: 1) /***********************************/ public class Test{ public static void main(String[] args) { Object a = new Object(); Object b = new Object(); Object c = new Object(); Object d = new Object(); d=c=b=a; d=null;
} } /***********************************/ How many objects will be eligible for garbage collection after line d=null;?
2) If object obj1 is accessible from object obj2 and object obj2 is accessible from object obj1, then obj1 and obj2 are not eligible for garbage collection. I thought the statement was right and why is it wrong? Why circular references do not prevent objects from being gargage collected? Thanks for help Kevin
Kevin
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
1) /***********************************/ public class Test{ public static void main(String[] args) { Object a = new Object(); Object b = new Object(); Object c = new Object(); Object d = new Object(); d=c=b=a; d=null;
} } d=c=b=a; The above line makes all the four variables reference the same object - object a; It means the other three objects which were originally referenced by the variables d,c & b become unreachable. So they are subject to garbage collection. "d=null" does not have any effect on the number of objects to be garbage collected because even though d is set null, the other three variables still have a reference to the object a;
/***********************************/ 2) If object obj1 is accessible from object obj2 and object obj2 is accessible from object obj1, then obj1 and obj2 are not eligible for garbage collection. I thought the statement was right and why is it wrong? Why circular references do not prevent objects from being gargage collected? The answer to the previous question should explain this.
kfu
Greenhorn
Joined: Aug 08, 2000
Posts: 8
posted
0
Vasansrini, Thanks for the help. Kevin
Doit
Ranch Hand
Joined: Aug 03, 2000
Posts: 169
posted
0
d=c=b=a; means i think here all objects are refering to d . as all assignements are done from right to left. I am bit confused here. Please help. - Thanks
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
You got the order right. They are all referencing a, not d. Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
in the sentence d=c=b=a;; all the 3 references [d,c,b] are pointing to the same Object to which reference a is pointing.soon after this line execution the 3 objects which were earlier referenced by b,c,d will be eligible for garbage collection cuz Norefrence is referencing to them and hence their referece counter dropped to ZERO.This is TRUE even after d= null ; though it is redundant and does not have any effect on garbage Collection.