File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question on MindQ no. 37 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 "GC question on MindQ no. 37" Watch "GC question on MindQ no. 37" New topic
Author

GC question on MindQ no. 37

Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
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.
The answer given are a, b, c. I think the answer should only be a and c since when resurrected object is garbage collected, the finalize method will not be called. finalize() method will only be called the first it is garbage collected.
Please clear this for me.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I feel that the answers given are correct. Regarding the option 2 - finalize() method does not actaully cause GC; it is just that if and when an object is about to be GC'ed, the finalize method will be executed first. Also if the JVM never decides to GC an object, it won't run finalize(). If you invoke finalize yourself , this won't result in any GC.
Hope this helps!!
Regds,
Milind


[This message has been edited by Milind (edited April 03, 2000).]
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
Thanx for answering Milind...
I know that calling finalize will not cause the object to be GC'ed. But in statement 2, it says "The finalize method is ALWAYS called before an object is garbage collected" which is not true since the finalize method will only be called the first time an object will be garbage collected. Note that the finalize method can resurrect an object by making some references to be pointing to it again. For example, by using a static field of a class to point to it (Myclass.ref = this ;) in the finalize method. In this case, the object that is supposed to be GC'ed will not be GC'ed (). What I mean by first time is that the next time this resurrected object will be garbage collected, the finalize method will not be called. IMO this statement is true if we change it to "The finalize method is always called the first time an object is to be garbage collected."
Please give some comments... Thanks.

[This message has been edited by Jerson Chua (edited April 03, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I thought answer 'C' should be wrong .Acc. to Khalid's book, it is not necessary to invoke super's finalize method!
Originally posted by Jerson Chua:
Thanx for answering Milind...
I know that calling finalize will not cause the object to be GC'ed. But in statement 2, it says "The finalize method is ALWAYS called before an object is garbage collected" which is not true since the finalize method will only be called the first time an object will be garbage collected. Note that the finalize method can resurrect an object by making some references to be pointing to it again. For example, by using a static field of a class to point to it (Myclass.ref = this in the finalize method. In this case, the object that is supposed to be GC'ed will not be GC'ed (). What I mean by first time is that the next time this resurrected object will be garbage collected, the finalize method will not be called. IMO this statement is true if we change it to "The finalize method is always called the first time an object is to be garbage collected."
Please give some comments... Thanks.

[This message has been edited by Jerson Chua (edited April 03, 2000).]

joy
Greenhorn

Joined: May 15, 2000
Posts: 16
jerson,
could you give me the link to the mindq mock exam?
regds,
che
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
It's now a dead link. Email me at jerson@rocketmail.com and I'll reply to you with the attachment.
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
It's now a dead link. Email me at jerson@rocketmail.com and I'll reply to you with the attachment.
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
C is correct. Consider a class that you extend which opens a file and close the file in it's finalize method. If you don't invoke super.finalize, you may correctly finalize your own part of the object, but the superclass' part will not get finalized.
Jerson
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
I have also written this test yesterday & I also feel the option 3 is wrong.
Moderators please help.
tvs sundaram
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
Hi,
I agree with tvs , option 3 is wrong.
Check the explanation from JQPlus Study Notes...it may clear ur doubts...


1)All objects have a finalize method as it is implemented in the Object class. But unlike constructors, finalize() does not call super class's finalize(). So, it is advisable (NOT REQUIRED) to put super.finalize() in the code of your finalize() method so as to give a chance to the super class to cleanup it's resources.

2)The order in which finalize methods are called may not reflect the order in which objects are destroyed.

Thanx

[This message has been edited by swati bannore (edited August 19, 2001).]
[This message has been edited by swati bannore (edited August 19, 2001).]


Swati Kale
SCJP
SCWCD
 
Don't get me started about those stupid light bulbs.
 
subject: GC question on MindQ no. 37
 
Similar Threads
garbage collection question.
garbagte Collection
garbage collection
mindQ no.37
Threads question from MindQ