• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collector  RSS feed

 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My question is

"An object becomes eligible for garbage collection when its reference count is zero i.e. no live thread can access that object".

But before the object is garbage collected Garbage collector calls the finalize() method on the object and finalize() is an instance

method which can be called only through object references.

SO if there is no reference how garbage collector calls the finalize method?

Thanks.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rakesh kankavlikar wrote:SO if there is no reference how garbage collector calls the finalize method?

GC is a thread, so when it comes to know that there is no longer any valid reference referring to the object, he call its finalize() and freed the heap space occupied by that object. So the thing is, GC has some mechanism to access that object, may be storing its reference somewhere, like virtual pointer table but its just my wild guess
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM itself, specifically the GC, still has a reference--that's how it can keep track of how many references etc. there are to the object.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, The point from David is very correct. Its the code that does not have the access to the object but the object will be still known and accessed by the jvm. Anyways its upto the jvm implementation and we need not have to worry much about it. What we need to know is we should not perform cleanup tasks inside the finalize method. For more reference on evils of finalize method please read Effective Java from Joshua Bloch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!