This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Garbage collection  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
I agree with Muhammad,
Question is poorly worded,
B seems to be a satisfactory answer to me
 
Valentin Crettaz
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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).]
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!