aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anyone explain me how there will be only one object eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anyone explain me how there will be only one object eligible for garbage collection?" Watch "Can anyone explain me how there will be only one object eligible for garbage collection?" New topic
Author

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

Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
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?
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 53
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.
Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
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??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

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.
Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
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.
}
}
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
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 ?
Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
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"
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
If you remain confused, you might consider taking the WhizLabs online SCJP6 preparation course.
It is very helpful, especially regarding these things.
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
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.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
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.
eric moraes
Greenhorn

Joined: Mar 02, 2013
Posts: 1
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.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
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.

Bhavesh Saraswat
Greenhorn

Joined: Jan 20, 2013
Posts: 18

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.


OCPJP SE6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can anyone explain me how there will be only one object eligible for garbage collection?