• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Reg Garbage Collection

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
This is one of the questions from MindQ's Mock exam..

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 answers given are a, b, c.

I don't understand how answers a and c are correct.

Can any one explain me how a and c are correct.

Thanks in advance.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choice A is certainly false. All Java programmers should commit to common knowlegde the following: Although you can request that the garbage collector run, you can never force this action.

Choice B is true. The finalize() method will always be invoked once before the object is garbage collected. Also, if an uncaught exception is thrown by finalize(), the exception is ignored and finalization of that object terminates. The finalize() method is not invoked again. Note that the finalize() method might never be invoked for an object in its lifetime because there is no guarantee that it will get garbage collected.

Choice C is true. A class that includes a finalize method should explicitly call its superclass's finalize(). The key is to remember that finalize() in any class is overridden from Object's class. Unlike C++ destructors or even Java constructors, in Java finalize() is a method, and calls are not made through the inheritance chain.

Choice D is also certainly false and should also be written into your programming muscle memory.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer A is actually true. The wording is tricky on that question. System.gc() does explicitly invoke the Garbage Collector, however, it does not guarantee that it will actually run. The JVM will run the Garbage Collector when it wants to.
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh... That is interesting I understand and appreciate your distinction.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, System.gc() does NOT invoke the garbage collector at all.
It polls a request for the garbage collector to be run to the JVM, that's all it does.
Whether the gc actually kicks off as a result of that request is never a given.
Sadly this is an error repeated in many books and mock exams.

Problem is that the Javadoc for System.gc() is ambiguous. At one point it states bluntly "runs the garbage collector", at the next "suggest that the JVM expand effort to run ..." and then again "the JVM has made best effort to...".

answer c) is correct because it says "should". Were it to say "must" it would be wrong.
The JLS clearly states that calling super.finalize() is best practice and encouraged but that it's not enforced.
 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The argument on this question has been made many times on this message board regarding the meaning of the term "invoke". Does invoke mean to call the garbage collector or run the garbage collector? You'll see arguments made both ways in this forum.

Two things are for certain:
1. MindQ's question is ambiguous at best.
2. You can never Force the garbage collector to collect garbage.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple of related discussions going on here:

First of all, the GC portion of the exam often trips people up, not because the concepts are that hard but because the wording is so precise and important. So here are a couple of things to keep in mind, and if you have these concepts clearly in your brain it'll help you navigate through the tricky exam questions:

1 - As has been stated in this thread, you can request that the JVM perform a GC, but it's only a request. There are never any guarantees.

2 - Finalize only runs once per object! If your finalize method has logic that resurrects your object, it's possible for an object to be restored to life, used, and then later become a candidate for GC again. The second time around, finalize will NOT be run.

3 - Be cautious about mock exam questions concerning the GC. In this area of the exam, precise wording is an essential aspect of any question, and most people who make the mock exams don't put their questions through the same level of rigorous analysis that the actual Sun exam questions go through. The GC questions that are pretty safe are those that give code listings and ask you when and if objects become ELIGIBLE for the GC.

4 - While this one isn't on the exam, it's important... don't put any important logic in a finalize method! In fact, we recommend that you don't use finalize at all. I suppose if you're goofing around and want to see when the GC runs you could mess around with finalize, but once again, it's NEVER guaranteed to run.

HTH,

Bert
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic