my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC doubt" Watch "GC doubt" New topic
Author

GC doubt

Jain Neeraj
Ranch Hand

Joined: Aug 09, 2001
Posts: 35
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
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
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 ]

"Luck is when opportunity meets preparation"
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 09, 2001
Posts: 35
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

Joined: Sep 26, 2003
Posts: 90
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 ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Oct 12, 2000
Posts: 5093
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.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC doubt