Win a copy of Terraform in Action this week in the Cloud 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

MindQ (Q.37) GC

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


37. 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 the correct answers are a,c.
b incorrect --> since finalize() is not ALWAYS called
Am I on the right track? Thanks.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) The garbage collector can be invoked explicitly using a Runtime object.
You can't invoke the GC yourself -- you can "request" that the GC be run, but its never guaranteed so don't depend on it!
System.gc()


public static void gc()
Runs the garbage collector.
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()


[ October 31, 2003: Message edited by: Jessica Sant ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i fully agree with this coz all objs r kids of Object and Object class has the default finalize method wich is always invoked if u havent overidden it in the class extending Object.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In some ways, b is correct.
The finalize method is always called whenever an object is garbage collected, except if that object is resurrected. Finalize method is called only once, so if you resurrect an object, its finalize method will not be called again.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alton Hernandez:
In some ways, b is correct.
The finalize method is always called whenever an object is garbage collected, except if that object is resurrected. Finalize method is called only once, so if you resurrect an object, its finalize method will not be called again.


Think about it... finalize is always called before an object is gc'ed. that does not mean it is immediately called before gc. But there will NEVER be a case where an object is gc'ed and finalize is not called once before it happens.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is not correct. One of the super class should have done something that requires a call to finalize, for it to be invoked. Shouldn't they?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:

Think about it... finalize is always called before an object is gc'ed. that does not mean it is immediately called before gc. But there will NEVER be a case where an object is gc'ed and finalize is not called once before it happens.


Hi Thomas,
I don't quite get what you mean. Are you pointing out an error in my statement? Could you elaborate further?
My understanding is that when an object is gc'ed, its finalize method is called. Unless that is a resurrected object, in which case, its finalize method will not be called again.
Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic