Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is finalize() call guranteed?

 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is it will compile and print multiple "Finalising ...." statement
 
Jerret Halter
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic