• 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 question on MindQ no. 37

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]


 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jerson,
could you give me the link to the mindq mock exam?
regds,
che
 
Jerson Chua
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also written this test yesterday & I also feel the option 3 is wrong.
Moderators please help.
tvs sundaram
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic