Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Explaination on GC in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explaination on GC in Java " Watch "Explaination on GC in Java " New topic
Author

Explaination on GC in Java

AmritP Singh
Greenhorn

Joined: May 10, 2012
Posts: 20
Hi all,

here is a code from kathy Sierra





When line 16 is reached, how many objects will be eligible for garbage collection?

My Question is Why a1 will not be eligible for GC after line 16.

Thanks

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

AmritP Singh wrote:
My Question is Why a1 will not be eligible for GC after line 16.


Because a1 is a variable, and variables don't get GCed; only objects do.

Now, if you mean "Why is the object that a1 initially pointed to not eligible for GC at line 16?" then the answer is that it is eligible. It somebody told you it's not, they were mistaken.
AmritP Singh
Greenhorn

Joined: May 10, 2012
Posts: 20
Hi Jeff

Thanks for your Quick Reply,

As per the explanation given at Kathy Sierra , it says that only one Object is Eligible for GC and that is b1.

I Actually mean a1 because a1 has the object for Alpha Class.. see Line 11.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

AmritP Singh wrote:Hi Jeff

Thanks for your Quick Reply,

As per the explanation given at Kathy Sierra , it says that only one Object is Eligible for GC and that is b1.


She's wrong. Or I am. Take what you've learned about GC and try to determine for yourself which one of us is correct. If the object that was initially pointed to by a1 is not eligible, then there must be some reachable reference to it. If you can find that reference, then you know that the book is correct.


I Actually mean a1 because a1 has the object for Alpha Class.. see Line 11.


Yes, I realized that. I was just trying to make the point that it's important to understand the distinction between variables and the objects they point to.
anindya halder
Greenhorn

Joined: Oct 03, 2012
Posts: 7
Hi AmritP,
I will try to explain you...hope you understand.....

Look carefully at class Alpha....its STATIC beta b1.

because b1 is a static variable of the class,its not confined to one object but to the whole class. So a1.b1 is same as a2.b1 ...Actually its Alpha.b1.

When we are writing a1.b1 = b1 ,implicitly a2.b1 is also referring to the object referred to by b1 (i.e a2.b1 = b1)
(please look carefully.....same names have been used to confuse us........ a1.b1 and and a2.b1 refers to the static variable of the class Alpha...........and b1 is the object of class Beta).


And so you can realize now that though we are nulling references a1,b1 and b2.......still a2.b1 is referring to b1 and therefore the object is not elligible for gc.

Hope this helps.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

anindya halder wrote:
And so you can realize now that though we are nulling references a1,b1 and b2.......still a2.b1 is referring to b1 and therefore the object is not elligible for gc.


His question was why the object that was originally pointed to by a1 is not eligible for GC. He wasn't asking about b1. As I already explained to him, the object he's asking about is eligible. Please pay closer attention to the question and the existing responses in the future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explaination on GC in Java