finalize() method is linked with garbage collection in java.
Suppose there is an object and you want some code (or perform some task) to run before that object is collected by garbage collector. You can put that code in the finalize() that all class will inherit from class Object. It means finalize() is defined in java.lang.Object class.
Note : The task might be of releasing any system resource or closing some connection that is left open.
For any given object, finalize() is called only once(at most) by garbage collector before the object is about to get garbage collected. Now if the object somehow revives itself during this call then finalize would not be called gain.
Issues with finalize()
It might seem a good idea to depend on finalize() and put some code inside it and rest assured that it would run before the object gets garbage collected. It's good until you come to know that its not guaranteed that finalize would be called by garbage collector or when it will be called by garbage collector. Sometimes it may also happen that object has to wait indefinitely before its finalize is called. Now lets assume that finalize gets called but then again there is no guarantee that object would immediately get recollected. Hence the time of execution of code inside finalize() remains suspicious. And the timing may vary from JVM to JVM. Thus relying on finalize() doesn't seems to be a good idea.
Declaration :
protected void finalize()
finalize() is declared as protected but why is something whose answer I am still searching...
Suppose there is an object and you want some code (or perform some task) to run before that object is collected by garbage collector. You can put that code in the finalize() that all class will inherit from class Object. It means finalize() is defined in java.lang.Object class.
Note : The task might be of releasing any system resource or closing some connection that is left open.
For any given object, finalize() is called only once(at most) by garbage collector before the object is about to get garbage collected. Now if the object somehow revives itself during this call then finalize would not be called gain.
Issues with finalize()
It might seem a good idea to depend on finalize() and put some code inside it and rest assured that it would run before the object gets garbage collected. It's good until you come to know that its not guaranteed that finalize would be called by garbage collector or when it will be called by garbage collector. Sometimes it may also happen that object has to wait indefinitely before its finalize is called. Now lets assume that finalize gets called but then again there is no guarantee that object would immediately get recollected. Hence the time of execution of code inside finalize() remains suspicious. And the timing may vary from JVM to JVM. Thus relying on finalize() doesn't seems to be a good idea.
Declaration :
protected void finalize()
finalize() is declared as protected but why is something whose answer I am still searching...
No comments:
Post a Comment