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

finalise method of Garbage Collection

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I came across this question in an interview :

I have an object reference = null;
Hence it is eligible for gc. Before the object is garbage collected, finalise method is called. What if in the finalise method i assign the reference a new object.

Is it still eligible for garbage collection?



Thanks.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
References aren't GC'ed. Objects are.

If an object doesn't have any valid references any more it can be GC'ed. If a reference to that object is created in the finalize method it will not be GC'ed because
it has a valid reference to it.
 
Deeps Mistry
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:References aren't GC'ed. Objects are.

If an object doesn't have any valid references any more it can be GC'ed. If a reference to that object is created in the finalize method it will not be GC'ed because
it has a valid reference to it.




Thanks so much!!!
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But because objects will only be finalized zero or one times, the object will not be garbage collected or finalized anymore after that, even if there are no more references. This will lead to memory leaks, and therefore you shouldn't do this.
 
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but it may not be GC-ed properly because the finalise() method is not called twice. If you re-create a reference and another attempt at garbage collection is made, finalise() has already been called, so it won't be called again.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic