aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is finalize() call guranteed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is finalize() call guranteed?" Watch "Is finalize() call guranteed?" New topic
Author

Is finalize() call guranteed?

S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
This question is found in www.jchq.net


But i feel with garbage collector nothing can be assured.
So when we are sure about the garbage collection, then what is the possiblity that finalize will be called?
GC is last thing to run before OutOfMemory Error but is that assured?
What is the correct answer?
Muralitharan Pannerselvam
Greenhorn

Joined: Jun 07, 2006
Posts: 15
the answer is it will compile and print multiple "Finalising ...." statement


Thanks and Regards,<br />Muralitharan P
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
finalize() is called when an object is ready to go to the GC and is only called ONE time on that object. In this instance you are creating multiple object but you are not assigning them to any reference so they are sent to the GC and finalize() will be called.

So when we are sure about the garbage collection, then what is the possiblity that finalize will be called? it is always called
GC is last thing to run before OutOfMemory Error but is that assured? well yes/no I would say, try creating an bst using recursion, several layers deep you'll get the out of memory error
What is the correct answer? B


Jerret Halter<br /> <br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.<hr></blockquote>
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
Yes answer B is correct.
But is that assured that in all circumstances that GC will run before throwing OutOfMemory Error? I think its not sure.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
On the site I include the following explanation of the answer to that question

"Because of the endless creation of anonymous instances of class Box, eventually the runtime environment will garbage collect those objects causing the finalize method to be run. It is not possible to predict when this will happen, but assuming you do not have an infinite amount of RAM (generally a fair assumption) then eventually the objects will be garbage collected. Under my version of Java 1.4 running under Linux it happens within 1 minute."


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
 
subject: Is finalize() call guranteed?
 
Similar Threads
need some clarification....
need help
question from marcus green
Reg Garbage Collection
finalize method behaviour