aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes MindQ (Q.37) GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "MindQ (Q.37) GC" Watch "MindQ (Q.37) GC" New topic
Author

MindQ (Q.37) GC

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

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

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Deependra Sharma
Greenhorn

Joined: Oct 31, 2003
Posts: 14
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.


DP Sharma,<br />Software Engineer,<br />India.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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?


SCJP2. Please Indent your code using UBB Code
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MindQ (Q.37) GC
 
Similar Threads
garbage collection question.
mindQ no.37
MindQ's 37 mock
GC problem, MindQ #37
garbage collection