• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
}
}
 
Bartender
Posts: 2445
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 327
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you remain confused, you might consider taking the WhizLabs online SCJP6 preparation course.
It is very helpful, especially regarding these things.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2445
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2445
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 18
Netbeans IDE Oracle Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic