• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is finalize() call guranteed?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is it will compile and print multiple "Finalising ...." statement
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
reply
    Bookmark Topic Watch Topic
  • New Topic