Forums Register Login

Can anyone explain me how there will be only one object eligible for garbage collection?

+Pie Number of slices to send: Send
Given:
3. class Beta { }
4. class Alpha {
5. static Beta b1;
6. Beta b2;
7. }
8. public class Tester {
9. public static void main(String[] args) {
10. Beta b1 = new Beta(); Beta b2 = new Beta();
11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
12. a1.b1 = b1;
13. a1.b2 = b1;
14. a2.b2 = b2;
15. a1 = null; b1 = null; b2 = null;
16. // do stuff
17. }
18. }
When line 16 is reached, how many objects will be eligible for garbage collection?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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??
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
}
}
1
+Pie Number of slices to send: Send
Draw a diagram:
i2 -> o , where o is an object
i3-> o1 , where o1 is an object
i4- > o2, where o2 is an object

i2.i -> i3 -> o1
i3.i -> i4->o2
i4.i-> i2 -> o

up to this point, i2.i is being assigned to o1 and i3 is being assigned to o1.

i2.i-> i3 means i2.i and i3 are refering to the same object.

Agree ?
+Pie Number of slices to send: Send
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"
1
+Pie Number of slices to send: Send
If you remain confused, you might consider taking the WhizLabs online SCJP6 preparation course.
It is very helpful, especially regarding these things.
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Given:
3. class Beta { }
4. class Alpha {
5. static Beta b1;
6. Beta b2;
7. }
8. public class Tester {
9. public static void main(String[] args) {
10. Beta b1 = new Beta(); Beta b2 = new Beta();
11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
12. a1.b1 = b1; <- HERE a2.b1 assing to b1 like Alpha.b1 (static)
13. a1.b2 = b1;
14. a2.b2 = b2;
15. a1 = null; b1 = null; b2 = null;
16. // do stuff
17. }
18. }
When line 16 is reached, how many objects will be eligible for garbage collection?


b1 is shared by alpha objects because is static
just one object is elegible, a1 object.
+Pie Number of slices to send: Send
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.

2
+Pie Number of slices to send: Send
 

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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1592 times.
Similar Threads
Garbage collection program really confusing me?
K&B Self Test Chp3 Q11
Problems with garbage collection
Garbage Collection query.
K&B SCJP6 Chapter 3- Q11 a GC question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:33:08.