• 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC doubt

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got a question on GC in some practice Test :

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.
e) The file pointer will be positioned immediately after the last character of the file.


Answer guide says the correct answers to be a,b and c
I'm not sure why a and c should be correct,
Can anybody help please.

Thanks
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jain Neeraj:

Answer guide says the correct answers to be a,b and c
I'm not sure why a and c should be correct,
Can anybody help please.

Thanks


(a)
Garbage collector can be invoked by using System.gc() or Runtime.getRuntime().gc()

(c)
The point is that finalize methods are not automatically "chained" - if you subclass a class that has an important finalize, your finalize should call it. Obviously if you don't know what the superclass finalize does, you should call it to be safe.

By contrast, When you create an object with the new keyword, the superclass constructor(s) are called first. This is automatically "chained".

Chandrasekhar
SCJP
[ October 01, 2004: Message edited by: Chandra Sekhar ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a)
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. One of the things you can do is ask the JVM to initiate garbage collection, which it may or may not do. To do this you can call Runtime.getRuntime().gc(). System.gc() does the same trick.

For c) A class may be subclassed from a base class which has its own finalize method. When an object is being finalized the finalizing thread does not check for this. So the finalize method of the subclass must call its parent class' finalize to ensure that the complete object has all its finalize methods run.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra and I overlapped, I type with only two fingers

Regarding c) it's an interesting point to consider when you must call super.finalize(). Before your finalization or after? That is the question. As Chandra mentioned constructors are chained. The base constructor is always called before the subclass constructor. For finalizers you have the choice and you must consider carefully when you do call super.finalize.
 
Jain Neeraj
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for a prompt reply,
in case of c) my doubt is clear
But a) confused because it used the word "explpicitly" to invoke the gc.

Thanks,
Neeraj
 
Chandra Sekhar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:

Regarding c) it's an interesting point to consider when you must call super.finalize(). Before your finalization or after? That is the question. As Chandra mentioned constructors are chained. The base constructor is always called before the subclass constructor. For finalizers you have the choice and you must consider carefully when you do call super.finalize.




Thanks Barry ...for adding comment to my post


Again for (a)
You can explicitly invoke the garbage collector using System.gc() or Runtime.getRuntime().gc() BuT there is no guarantee that gc will run.

Chandrasekhar S.
SCJP
[ October 01, 2004: Message edited by: Chandra Sekhar ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) is incorrect. You can invoke a method which will REQUEST the GC to be run, you cannot directly invoke the GC itself.
c) is correct. If you fail to call super.finalize() there may be operations which need be performed but aren't. In contrast to (some) constructors the compiler will not assume a call to super.finalize() if not provided.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Sun Java 1.4 Runtime API says:

public void gc()

Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.



It says rather explicitly there: "Runs the garbage collector". So who do you believe? Also: "When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects."

Got the source, Luke?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's a native method, so no :roll:

Of course it first says "runs the garbage collector" and on the next line "Calling this method suggests that the Java virtual machine expend
"... so I guess it depends on what you call the garbage collector (the function called from Runtime.gc() or the process that actually collects the garbage.

AFAIK the exam expects you to know that calling Runtime.gc() is not guaranteed to have any effect.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To know whether the answer is correct, you have to interpret "can be invoked". Does invoked mean "you can call gc()"? sure, so the answer is correct in that case--you CAN always make the method call. But does it mean the actual gc subsystem WILL run as a result?

(Although, FYI, I think that virtually all of Sun's own JVM's have interpreted a call to gc() to mean, do it NOW; it is not a suggestion in Sun's implementations, although I'm not sure about the newer ones.)

But the spec does not guarantee that for *all* implementations, gc() will always mean that garbage collection happens. So, for the exam, you should assume that calling System.gc() is considered a "suggestion".

Answer B is also interesting (and works):
"The finalize method is always called before an object is garbage collected."


But what would happen if it changed to:
"The finalize method will always run before an object is garbage collected." ?

I'll leave that to you...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic