Poonam Jain
Drawbacks of finalize method????
By Poonam Jain in Java on Apr 18 2009
  • Suresh Ravi
    Aug, 2009 31

    We should avoid writing a Finalize method (destructor) unless our object controls some unmanaged resource (such as file handles, window handles, or database connections) other than managed memory. If our object merely has references to other managed code objects, garbage collector will take care of freeing all the memory and other objects correctly.

    The reason to avoid finalizers, unless you absolutely need them, is
    • Objects with finalizers take much longer to garbage collected than do objects without, so don't override Finalize “just to be safe.” 

    • Before the object that has a finalize method can be garbage collected, CLR will do a lot of time consuming work on this. So think twice before you use the finalize method, as finalizing objects can needlessly hurt your program's performance.

    • We cannot really predict when the finalize method will be called. It will be called during garbage collection, and time to collect the garbage is decided by the .net runtime by using optimization algorithms.

    To surpass the disadvantage of finalizers another method is provided in .net framework i.e. Dispose. Using Dispose will not hurt your programs performance or kill the runtime time


    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS