• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MindQ (Q.37) GC

 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • 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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Deependra Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • 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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic