This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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?
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>
Joined: Mar 19, 2006
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.
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."