• 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 and finalize()

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some doubt about the following question.
Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
I think b, c.
The finalize() method will eventually be called on every object -true or false.
Ans is given False, there is no guarantee that an object will be GCed.
I think it's true.
Please correct me if I am wrong.
Thanks
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a system.gc method to kick start garbage collection.
Dan
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the answers are a, b, and c.
A programmer can suggest using the JVM garbage collection by using one of the followin method calls:
System.getRuntime().gc()
System.gc();

In regards to the second question. The garbage collector will not always be run even if we explicitly call it. Keep in mind that before the garbage collector is fun that finalize will be run; and as a result, all objects are not guaranteed 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
The garbage collector can not be invoked. It can be requested but that request does not have to be honored.
There is no guarantee that finalize() will run. finalize() only runs when an object is gc'ed. If the JVM can return all its memory without running gc then finalize() will not be run.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


b) The finalize method is always called before an object is garbage collected.


This is not exactly true. The gc will call <code>finalize()</code> before an object is garbage collected but it is guaranteed to be run once and only once. If an object is being gc'd and it's finalize method assigns it's reference to another active object; and at some later point it again becomes available for gc, it's <code>finalize()</code> will not be called a second time.
See the API for Object.finalize()
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic