Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many eligible for gc?

 
Rajesh Shinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Good Example, Thanks Rajesh Shinde for sharing......
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really good example i completely skips the static variable.
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the graph. It seems no object is eligible for GC. Is this right .
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajesh Shinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. Got it. Thanks for the reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic