• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Garbage Collector Question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please provide the answers for the following question.

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.


My knowledge says (b) is the only b is correct. But the key says a,b,c are all correct.

My explanation:

a)This is wrong because the System.gc() and Runtime.gc() just allow us to request the gc for collection but they wont guarantee any.

c) c is wrong because the finalize() method in Object doesnt have any specific implementation. So there is no reason in calling super.gc()
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) Consider this from the System.gc() API documentation:

When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.



That is saying that the garbage collector will run (but it does not specify that any particular objects will be collected).


c) What about a class C that extends class B that extends class A that extends class Object? Classes B and A may each have a finalize() method that must be called during the finalization of an instance of class C. To make sure that the finalize method is run you should use super.finalize() in the finalize methods. Note: you do not call super.gc() as you have written above.
[ January 31, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Barry Gaunt,
Can you please make the option (a) more clear.
AND
IS "invoking garbage collector explicitly " same as "forcing the garbage collector"???

Q> Can we force garbage collector? [T/F]
 
kishore Nekkanti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Thanks for reply.

My apologies about option c, I want to mention super.finalize().

But can you please confirm "is it mandatory to call super.finalize() or is it a good practice to call super.finalize(), while overriding finalize() method".
Kindly suggest the answers for the above question also.

Thanks,
Nandakishore

P.S.: My intention is not to let any body think. However i follow your advice.
 
reply
    Bookmark Topic Watch Topic
  • New Topic