At line 15 b1 and b2 are assigned to null, also a1 assigned to null which holds b1. So after this line all object except a2 would be eligible for garbage collected.
a1, b1 and b2 are all references, not objects. Only objects get garbage collected. Four objects are created, and you need to work out which still have live references to them.
So in this below given program how many objects will be eligible for GC??
1)public class Island {
2)Island i;
3)public static void main(String [] args) {
4)Island i2 = new Island();
5)Island i3 = new Island();
6)Island i4 = new Island();
7)i2.i = i3;
8)i3.i = i4;
9)i4.i = i2;
10)i2 = null;
11)i3 = null;
12)i4 = null;
// do complicated, memory intensive stuff
The Thing Which I'm Not Getting Is,Is the value of i2.i is being assigned to i3 or the value of i3 is being assigned to i2.i. }
}
Here i2, i3, and i4 are references which are null at the end..and hence they are eligible for GC but as far as I know only objects are eligible for GC.."BIG CONFUSION"
Tushar Gosalia wrote:So in this below given program how many objects will be eligible for GC??
1)public class Island {
2)Island i;
3)public static void main(String [] args) {
4)Island i2 = new Island();
5)Island i3 = new Island();
6)Island i4 = new Island();
7)i2.i = i3;
8)i3.i = i4;
9)i4.i = i2;
10)i2 = null;
11)i3 = null;
12)i4 = null;
// do complicated, memory intensive stuff
The Thing Which I'm Not Getting Is,Is the value of i2.i is being assigned to i3 or the value of i3 is being assigned to i2.i. }
}
i3 is being assigned to i2.i, after this assignment, i2.i will refer to the same object that i3 refers.
Tushar Gosalia wrote:Here i2, i3, and i4 are references which are null at the end..and hence they are eligible for GC but as far as I know only objects are eligible for GC.."BIG CONFUSION"
Yes, at the end i2, i3 , i4 are all refering to null. All those objects in my post, o, o1 and o2 are isolated at the end. These objects are eligible for GC.
Yes. Only one object is eligible for GC.
Diagram:
b1-> o, which is new Beta()
b2-> o1, which is another new Beta()
a1-> 02, new Alpha.
a2-> 03, new Alpha.
After this a1=null, b1=null, b2 =null,
o is referred by Alpha.b1
o1 is referred by a2.b2
o2 is not referred by any references.
o3 is refered by a2.
Tushar Gosalia wrote:Since at line 15 a1, b1, b2 are assigned to null,so why they are not eligible for GC and at line 5 b1 is static does it makes any difference??
static DOES make difference here in retaining object references!!! Remember, static variables are not associated to any specific object. Indeed they are common to all objects during the span of program execution time. This is why static variables are accessed through class name. What is confusing is static variables can also be accessed by an object reference, this makes them think like they associated to an object but infact they are not.
static variables(object references) remain valid even after no object is present of the same type. static variables are created when class loads into memory and lasts till the jvm is running.
So we can say that if we assign some object reference to a static variable than it means that object will never be garbage collected because its reference is present in a static variable which never dies. There is only one way to make that object available for GC , remove the reference of the object from the static variable.
I hope it is clear to you now.
Post by:autobot
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking