File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explaination on GC in Java

 
AmritP Singh
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic