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

GC (javaprepare.com)

 
Ranch Hand
Posts: 270
  • 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 related to Garbage Collection are correct. Select the two correct answers.
a. It is possible for a program to free memory at a given time.
b. Garbage Collection feature of Java ensures that the program never runs out of memory.
c. It is possible for a program to make an object available for Garbage Collection.
d. The finalize method of an object is invoked before garbage collection is performed on the object.


The answer given is c,d.
I have a question regarding d.
"The finalize method is never invoked more than once by a Java virtual machine for any given object." According to this statement option d is incomplete. Or may be I do not understand what the above statement means..
Any thoughts? Thanks.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oblect class API:


The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, Cathy. If finalize runs but the object is not gc'ed then finalize will not run later when the object is gc'ed. But "d" is correct too. Before an object is gc'ed, finalize will run. The object can not be gc'ed unless finalize has run. It may be that finalize ran long before the object was gc'ed but the question doesn't state "immediately" before garbage collection.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It may be that finalize ran long before the object was gc'ed but the question doesn't state "immediately" before garbage collection.


I see your point Thomas.
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic