• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection problem-2

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


SCJP1.6 JAVA 6 CERTIFICATION Guide-310-065
by Katherine Sierra (Author), Bert Bates (Author)

pg-284
question-11
self test






When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5
Answer
B is correct. It should be clear that there is still a reference to the object referred to by
a2, and that there is still a reference to the object referred to by a2.b2. What might be
less clear is that you can still access the other Beta object through the static variable
a2.b1—because it's static.

first of all, how many objects would be created ?
i think
-two Beta class objects ,first refered by b1 and second refered by b2
-two Alpha class objects ,first refered by a1 and second refered by a2

so,when
a1 = null; b1 = null; b2 = null;
would be executed,object refered by a1,b1,b2 would be eligible for GC.

the answer as given in the book is obscure .please explain it.






 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear,

I am using ID just for reference and ---> means "pointing to "

b1---> Beta Object ID1
b2----> Beta Object ID2

a1----> Alpha Object ID3
a2-----> Alpha Object ID4

Alpha Class Static member (b1) ------> Beta Object ID1


Alpha Object ID3 (b2) --------> Beta Object ID1
Alpha Object ID4 (b2) ---------> Beta Object ID2


This is the situation till line 14.

Now after the execution of a1 =null; b2 =null b1=null , we have the following situation

b1 Beta Object ID1
b2 Beta Object ID2

a1 Alpha Object ID3
a2-----> Alpha Object ID4 // First Object saved

Alpha Class Static member (b1) ------> Beta Object ID1 // Second Object through A's class Static member , no relation with object. <Saved>


Alpha Object ID3 (b2) --------> Beta Object ID1 // Lost because reference to Alpha Object ID3 by a1 is lost through the statement a1=null; <LOST>

Alpha Object ID4 (b2) ---------> Beta Object ID2 // Third Object saved



So only 1 object is eligible for GC !!!


Thanks !!!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a1=null; b1=null; b2=null will execute only object referred to by a1
will be eligilble for GC.

b2 will not be eligible because b2 of a2 is pointing towards it(Line 14).

b1 will not be eligible because the static varable b1 of class Alpha objects is still
pointing to b1 (Line 12). Remeber static variable is shared by all the objects of
a class.
Therefore if you write a1.b1=b2 or a2.b1=b2 after line 12 then no object will
point towards b1 and then b1 will be eligible for GC but not now.

Hope this would help


 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i still have some queries

1.when a1=Null,would its instance variables exist ? i.e

static Beta b1;
Beta b2;

2.if the line a1.b2 = b1; is removed would still one object be available for GC ?


3.if Alpha class contains

Beta b1;
instead of
Static Beta b1;

then also would one object be available for GC ?

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. when a1=Null,would its instance variables exist ? i.e


Yes instance variables exist inside GC heap as long as An object is not eaten by GC. Eating means freeing up the space contained by these instance variables .

2.if the line a1.b2 = b1; is removed would still one object be available for GC ?



Yes because an object referred by b1 is eligible due to presence of static variable b1. You can access a2.b1 which essentially contains an object previously referred by b1.


3.if Alpha class contains

Beta b1;
instead of
Static Beta b1;

then also would one object be available for GC ?



No then 2 objects would be eligible as you set a1.b1=b1; and later a1=null;

But if you had done a2.b1=b1 then answer would have been 1 again.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ujjawal rohra wrote:When a1=null; b1=null; b2=null will execute only object referred to by a1
will be eligilble for GC.

b2 will not be eligible because b2 of a2 is pointing towards it(Line 14).

b1 will not be eligible because the static varable b1 of class Alpha objects is still
pointing to b1 (Line 12). Remeber static variable is shared by all the objects of
a class.
Therefore if you write a1.b1=b2 or a2.b1=b2 after line 12 then no object will
point towards b1 and then b1 will be eligible for GC but not now.

Hope this would help




Do you mean that b1 should be elegible for GC if you write a1.b1=b2 or a2.b1=b2 in line 12?.. but what about line 13 a1.b2=b1? We will find another reference in this line. Could you explain with more details your theory?

Thanks in advance.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do you mean that b1 should be elegible for GC if you write a1.b1=b2 or a2.b1=b2 in line 12?.. but what about line 13 a1.b2=b1? We will find another reference in this line.



No, b1 is not eligible for GC when a1.b1=b2 or a2.b1=b2 line 12.

First of all, b1 is a variable , not an object. Therefore, b1 is never eligible for GC. But the object that b1 refers to is eligible for GC if that object has no variable referring to it.

Look at a simpler example:


What object is eligible for GC after line 4?
When a1 is set to null, the object created in line 2 has no variable referring to it. It is eligible for GC.

How about the object created in line 1?
b1 is still referring to it, so it is NOT eligible for GC.

How about a1.b2?
No, b2 is a variable, not an object. It won't be GC.

If you have further questions, please feel free to ask. Also, you can take a look at the post "Garbage Collection" from yesterday to see more example.



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic