This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
reply
    Bookmark Topic Watch Topic
  • New Topic