• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Garbage collection

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I found this question in one of the mock exams.
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 is a,b,c.
Please explain.
Thank you
Porchelvi
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a. You may invoke the gc explicitely, though it is not guaranteed that it will run immediately.
b. this behavior is mandatory (see JLS)
c. in order to ensure proper finalization of objects the programmer should call the superclass's finalization method, to propagate finalization up the hierachy. This propagation is not done automatically by the compiler (like it is the case with constructors)
Val
[This message has been edited by Valentin Crettaz (edited September 23, 2001).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think "c" is the correct answer, as the word "should" is used, it is not mandatory/required to call finalize() of the super class.
As far "a" is concerned , it is also poorly worded, we CANNOT invoke gc, all we can do is to TRY to invoke and the rest is dependent on JVM, and the bahaviour of JVM is very unpredictable. Please someone correct me if I am wrong.
--Farooq
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Muhammad,
Question is poorly worded,
B seems to be a satisfactory answer to me
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
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 GC CAN be invoked though nothing is guaranteed, but it CAN be invoked !!
c. the modal "should" is used here in order to emphasize that it's a good practice and clean implementation to do so !!
I would be ok with your point if the verb was MUST, but should is ok !! When you write "should", you mean to give some piece of advice, and not to state some obligation...
Anyway let's not argue about wording, which is truly poor by the way !
Val
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right Val, gc can be invoked though nothing is guarnateed. In the exam I will go with the number of correct answers to determine which answers are or can be correct.
--Farooq
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a is correct because we can REQUEST to invoke garbage
collection explicitly by invoking either Runtime.gc().
b is correct because finalize method will be called just before the object is collected. This is just a notification mechanism
to tell that object is about to be collected. So, this will
be called only before collection.
c is correct, because finalize method will always be
an override of a superclass method. So finalize method should
invoke superclass's finalize method.
This process is having a significance, i.e., before making
an object available for garbage collection we should make
sure that all it is not having any references,
and d is not correct because garbage collection behavior will never be predictable.
--SaiMurali.
[This message has been edited by sai murali (edited October 03, 2001).]
 
Your buns are mine! But you can have this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic