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

GC

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?



1) An object will be garbage collected when it becomes unreachable
2) A reference will be garbage collected if it has null assigned to it
3) The finalize method will be run before an object is garbage collected.
4) Garbage collection assures that a program will never run out of memory.


I think finalize method will not be run always before GC, if it ran before for the same object. Anyone can explain?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize() method is only called just prior to garbage collection. It is not called when reference to object is lost. We cannot know when finalize will be executed because we don't know when object will be garbage collected.
 
mitja siska
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing. finalize() method is marked as protected in Object class. When we need it, we override this method. It is probably wrong to call finalize method in your code and it is also probably not to mark it ptotected.
But it is guaranteed that finalize() will be called just prior object is garbage collected. But it can happens that finalize() will never be called, if the object is never garbage collected. This is perfectly valid!
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize method will always run always before GC.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any bartender can explain?
 
reply
    Bookmark Topic Watch Topic
  • New Topic