• 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

How many eligible for gc?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 504
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Good Example, Thanks Rajesh Shinde for sharing......
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really good example i completely skips the static variable.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the graph. It seems no object is eligible for GC. Is this right .
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. Got it. Thanks for the reply.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic