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 When finalize() will be called? 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 "When finalize() will be called?" Watch "When finalize() will be called?" New topic
Author

When finalize() will be called?

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344


In the above example, how the finalize method is called automatically?


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
After the main method execution completed 2 objects are eligible for Garbage Collection process. Whena finalise method is called cant be guranteed


SCJP 5.0 77%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please, always mention the source of the mock questions you post.

finalize() will be called when the garbage collector attempts to garbage collect the object for the first time. Because objects are not always garbage collected, you cannot say definitely that finalize() will be called.


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

Joined: Nov 27, 2006
Posts: 52
Well the code shown above is actually an infinite loop. This loop creates a new Box object every time. If there was a mechanism in the loop to control it and limit it to run only once then what Satish said is true there would be two objects ready for GC at the end of the programs lifetime.

K&B (chapter 3) says that;

For any given object, finalize() will be called only once (at most) by the garbage collector.


Hope it helps.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The thing is, Greg, is that the garbage collector does not have to run at the end of a program. So the program could just return from main and no finalizers need be called.
Greg L Tonn
Ranch Hand

Joined: Nov 27, 2006
Posts: 52
Berry,

So if the program ends, returns from main, the objects still sit out there on the heap, right?

If that is the case when the GC does run does it would it even attempt to call the finalizer (even through I know it's not a guarantee)?

Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In the case of the program ending - and the JVM exiting - the heap is wiped out, the GC does not necessarily go and collect each object and run its finalizer. If an object is garbage collected then it is part of the contract that the finalizer must be run for that object. If the GC attempts to collect the object a second time the finalizer will not be run.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

So if the program ends, returns from main, the objects still sit out there on the heap, right?


When a program ends, causing the JVM to exit, all resources are cleaned up by the operating system. The OS doesn't know the difference between the heap, the string cache, the stack, etc., it is all just user memory.

The memory related to the JVM process are returned to free memory.

If that is the case when the GC does run does it would it even attempt to call the finalizer (even through I know it's not a guarantee)?


The GC is part of the JVM process. There is no "when the GC does run".

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When finalize() will be called?
 
Similar Threads
Question from Examsimulator
GC question
Is finalize() call guranteed?
need help
question from marcus green