aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many eligible for gc? 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 "How many eligible for gc?" Watch "How many eligible for gc?" New topic
Author

How many eligible for gc?

Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
Given:
class Beta { }
class Alpha {
static Beta b1;
Beta b2;
}
public class Tester {
public static void main(String[] args) {
Beta b1 = new Beta(); Beta b2 = new Beta();
Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
a1.b1 = b1;
a1.b2 = b1;
a2.b2 = b2;
a1 = null; b1 = null; b2 = null;
// do stuff
}
}
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

In book it says B. 1 is correct...
But as per my understanding:
object pointed by refernence a1 and object pointed by reference b1 should be eligible for gc..(i.e C. 2 should be correct)
Can anyone please make me understand this, as to why b1(Object) is not eligible for gc, as we do not have any reference pointing towards this object?


scjp6 processing.....
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Please use code tags while writing code


you must see that b1 in Alpha class is static that is it doesn't belong to any object of class but to the class itself and object referred by b1 in Tester class is assigned to this static b1 of Alpha class also. So, even after a1 is nulled the b1 of tester will still be reffered by static b1 of class Alpha and has will not be eligible to gc as static b1 is a live reference.


SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

A Good Example, Thanks Rajesh Shinde for sharing......


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Really good example i completely skips the static variable.
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Hi,

Neha Daga Wrote
you must see that b1 in Alpha class is static that is it doesn't belong to any object of class.


I understand, as static belongs to the class rather than object.

reffered by static b1 of class Alpha and has will not be eligible to gc as static b1 is a live reference.


Ok So since a1.b1 is refering to b1. The static variable b1 is not eligible for GC. What I am not able to understand is how two objects are eligible for gc. a1.b2 losses its reference to b1 since reference variable a1 is made null.. agreed. a1.b1 is still refereing to the same object which a1.b2 was refering to. If this is the case then no object should be eligible for GC.
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

here is the graph:


As you see member b1 is a static method of Alpha class so it will not be bonded to a single instance of Alpha class, It will be shared by all the instances

In the code a1 will be null so it will be eligible for garbage collection , b1 and b2 are also assigned null but the actual object being referenced by both of them is still having live references. b2 is still having reference from a2.b2 and b1 is still having live reference from a2.b1 ( because b1 is a static member and will be common to all the instances.
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
Looking at the graph. It seems no object is eligible for GC. Is this right .
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

At statement a2.b2 = b2;, the memory structure looks like this
After the three references are set to null, the memory structure looks like this
So 1 object is eligible for GC...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

No

In the code a1 will be null so it will be eligible for garbage collection , b1 and b2 are also assigned null but the actual object being referenced by both of them is still having live references. b2 is still having reference from a2.b2 and b1 is still having live reference from a2.b1 ( because b1 is a static member and will be common to all the instances.


a1 will be eligible for garbage collection because it has been assigne null and not having any live reference.

Graph is showing the memory map before line 13 of the code. But at line 13 you are assigning null to a1 so it will be eligible for garbage collection but b1 and b2 are still having live references so they will not be eligible for Garbage collection. a2.b1 will implicitly get reference to b1 (because it's a static member).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

Welcome to JavaRanch.

Rajesh Shinde wrote:In book it says B.

Which book? Please note that when you post a question from a book, mock exam or other source, it's required to QuoteYourSources - in other words, tell us where you copied the question from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
Thanks for all your valuable comments and suggestions.
This was my first post, in future will make sure to follow the common practices, as suggested
Source : Sun Certified Programmer for Java 6 Study Guide.
So as i understand since Alpha.b1 is a static variable, so object referenced by it, will not be available for gc, but if we make a2=null, then object referred by static variable b1 might be available for gc as "An object is eligible for GC if it is not reachable by a live thread. It does not matter if the reference to the object is static."
Am i correct?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Rajesh Shinde wrote:So as i understand since Alpha.b1 is a static variable, so object referenced by it, will not be available for gc, but if we make a2=null, then object referred by static variable b1 might be available for gc as "An object is eligible for GC if it is not reachable by a live thread. It does not matter if the reference to the object is static."

But b1 is accessible to a live thread, so it cannot be eligible for eligible for GC. Static variables will be eligible for GC when their class is unloaded because otherwise they are accessible to any running thread...
Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
ok.. Got it. Thanks for the reply.
 
Consider Paul's rocket mass heater.
 
subject: How many eligible for gc?