File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection 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 "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
CAN SOMEONE PLEASE EXPLAIN THE ANSERS TO THIS QUESTION FOR MINDQ SAMPLE 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
P SOLAIAPPAN
Ranch Hand

Joined: Oct 20, 2000
Posts: 68
Hi Sarim,
The ans is (b) & (c)
The finalize() method is always called before an object is garbage collected.
This method is declared in Object class. In your program you can override it and include your own code in it. Within your own code you have to always include super.finalize() to invoke its superclass' finalize method. This basic thing you can find in any Java book's Garbage Collection Chapter.
solaiappan
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
a) is true. The garbage collector runs automatically, normally. But you can "kick it so that it will work on your demand".
Here is what the API says about the gc() method of the Runtime class:
"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 Java Virtual Machine has made its best effort to recycle all discarded objects.
The name gc stands for "garbage collector". The Java Virtual Machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.
The method System.gc() is hte conventional and convenient means of invoking this method."
b) is true also because that's the way the garbage collector works: it will always run the finalize() method of the object. Note that a finalize() method may set an object reachable again. But the object will no more have its finalize method executed more than once.
c) is true "should". If it would say that it must include the finalize method, then it would be false. It is a good practice to invoke the superclass finalize() method.
The finalize() method is defined in class Object, the grand parent of all classes. So it is not mandatory to override it.
d) is false. You never know for sure if an object will be garbage collected. You only know that an object may be garbage collected if it is unreachable (i.e. no other objects refer to it, it is set to null, or...).
------------------
Hope this helps.
Have a good day.
[This message has been edited by Serge Plourde (edited November 02, 2000).]
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
thanks a lot!
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
Hi All,
I think the only correct answer should be a & b.
a) The garbage collector can be invoked explicitly using a Runtime object.
True. But doesnt guarantee GC.
b) The finalize method is always called before an object is garbage collected.
True
c) Any class that includes a finalize method should invoke its superclass' finalize method.
False . NOte the word "should", it is advisible but not a "must"
d) Garbage collection behavior is very predictable
False. unpredictable.

Suggestions are Welcome.
Aruna
Supriya
Greenhorn

Joined: Oct 27, 2000
Posts: 9
Doubt on a gc question from Applied Reasoning test:
"Java's garbage collector will be invoked by executing System.gc()"
i think this is true.
but the answer says "it may or may not invoke".can anyone clarify.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Supriya, may I ask you to register with proper names?
You can read this post for more details. We are glad to see you here, just a little formality´┐Ż

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Hi every 1,
I think answer is A, B, C. As explained by Searge as it says that it should include the finalize method, so it is true as it is a good practice to invoke the superclass finalize() method. AS should means that the thing is better if its done, and must means that thing has to be done. I think i m not wrong.
thank u
Sadashiv Borkar
Ranch Hand

Joined: Jun 07, 2000
Posts: 49
Hi all,
I was just going through this questio and found that the correct ansers are b and c only and not a.

Serge Plourde
a) is true. The garbage collector runs automatically, normally. But you can "kick it so that it will work on your demand".
Here is what the API says about the gc() method of the Runtime class:
"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 Java Virtual Machine has made its best effort to recycle all discarded objects.
The name gc stands for "garbage collector". The Java Virtual Machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.
The method System.gc() is hte conventional and convenient means of invoking this method."

As serge has correctly said that it is mentioned in the API about the gc() method of the Runtime class that
"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 Java Virtual Machine has made its best effort to recycle all discarded objects.
but if you see carefully it says that it suggest and not forces the garbage collection thus cannot be invoked explicitly by the Runtime class.
I hope this is clear and correct.
cheers
Sada
 
Consider Paul's rocket mass heater.
 
subject: garbage collection