Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

GC problem, MindQ #37

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following question from MindQ #37:
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.
The answer is a) b) c) but I think only c) is correct, can any people give me detail explain since I alway has error on questions about gc? Thanks in advance..
Kai,
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think ONLY 'b' is correct. You do not HAVE to call the finalize method of the super-class from the sub-class finalize.
Any takers . . . ?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Thomas here.
(a) is wrong because you can suggest garbage collection using the Runtime object. Whether the GC actually runs or not cannot be predicted.

b) is the right answer.

(c) is wrong too because of the use of should qualifier here. Calling super.finalize() and it is highly recommended to ensure proper finalization of all the objects in the hierarchy. However, it is not a should-have condition. If you are sure it will not cause any resource leaks you may chose not to do it!
(d) - No No No!!
Ajith
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think a, b, and c are all correct.
a is correct because Java does provide the method System.gc() to invoke the GC explicitly, however, ther are no guarantees that the GC will run.
c is correct because the word "SHOULD" is not "MUST", it gives definite advice, high recommendation.
Just my opinion!
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About b) option,
a cite from Design Techniques (http://www.artima.com/designtechniques/index.html), the article
"Object Finalization and Cleanup":
"One more rule of thumb about finalizers concerns objects left on the heap at the end of the application's lifetime. By default, the garbage collector will not execute the finalizers of any objects left on the heap when the application exits. To change this default, you must invoke the runFinalizersOnExit() method of class Runtime or System, passing true as the single parameter. If your program contains objects whose finalizers must absolutely be invoked before the program exits, be sure to invoke runFinalizersOnExit() somewhere in your program."
so b) is wrong?
 
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
Hi Mapraputa,
The method System.runFinalizersOnExit() has been deprecated in Java 2.

Originally posted by Mapraputa Is:
About b) option,
a cite from Design Techniques (http://www.artima.com/designtechniques/index.html), the article
"Object Finalization and Cleanup":
"One more rule of thumb about finalizers concerns objects left on the heap at the end of the application's lifetime. By default, the garbage collector will not execute the finalizers of any objects left on the heap when the application exits. To change this default, you must invoke the runFinalizersOnExit() method of class Runtime or System, passing true as the single parameter. If your program contains objects whose finalizers must absolutely be invoked before the program exits, be sure to invoke runFinalizersOnExit() somewhere in your program."
so b) is wrong?


 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Erin!
I did not know runFinalizersOnExit() has been deprecated�
I wrote a small program to clarify this.

without System.runFinalizersOnExit(true) the program prints nothing, with System.runFinalizersOnExit(true); the output is:
second
first
My understanding:

�The finalize method is always called before an object is garbage collected.�

- true

�GC always starts before the application exit�

- false
Then the phrase

�If your program contains objects whose finalizers must absolutely be invoked before the program exits, be sure to invoke runFinalizersOnExit() somewhere in your program"

makes sense. I have only one unclear point: why is runFinalizersOnExit() deprecated?

[This message has been edited by Mapraputa Is (edited September 05, 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
Hi Mapraputa,
Here is some explanations from Core Java 2:
"There is a method System.runFinalizersOnExit(true) to gurantee that finalizer methods are called before Java shuts down. However, this method is inherently unsafe and has been deprecated in Java 2."
Hopes this will help.
Erin

Originally posted by Mapraputa Is:
[B]Thanks Erin!
I did not know runFinalizersOnExit() has been deprecated�
I wrote a small program to clarify this.

without System.runFinalizersOnExit(true) the program prints nothing, with System.runFinalizersOnExit(true); the output is:
second
first
My understanding:

�The finalize method is always called before an object is garbage collected.�

- true

�GC always starts before the application exit�

- false
Then the phrase

�If your program contains objects whose finalizers must absolutely be invoked before the program exits, be sure to invoke runFinalizersOnExit() somewhere in your program"

makes sense. I have only one unclear point: why is runFinalizersOnExit() deprecated?

[This message has been edited by Mapraputa Is (edited September 05, 2000).][/B]


 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again, Erin!
I wondered if API specification suggests to use some other method instead (as it usually does), but no, nothing is recommended instead of runFinalizersOnExit(). That seems strange, I thought finalizers are important part of object life cycle, but we do not have a guarantee that they will be called. All what API specification provides are explainations:


This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.


OK.
1. RunFinalizersOnExit() forces JVM to call finalizers before it exits.
2. JVM doesn't exit until all non-daemon) threads are finished.
3. By default all created threads are non-daemon.
All this means that I can safely use RunFinalizersOnExit() method as long as I do not make my threads daemon (pretty exsotic case, no?).
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to "A programmer's guide to Java Certification", the finalizer is only called once on an object. Page 252.
reply
    Bookmark Topic Watch Topic
  • New Topic