• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Garbage Collector

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic