Java Finalize method
Always in program that you make need some resources like file operations ,database connections .These resources are called non-Java resources and are need to freed once object associated with it is destroyed otherwise the heap size and connection pool keeps on growing.
So when you do not need an object anymore do not forget to release the non-Java resource.finalize() method provides an automatic way of releasing such resources this mechanism is called finalization.
By using finalization, you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.To add a finalizer to your class ,what you need to do is just add a finalize() method to your class as follows:-
public class FinalizeTest{
protected void finalize(){
}
}
Inside this finalize() method you must specify the actions that are needed to be performed before an object is recycled.
How finalize() method works:-
Its important to note the concept of JVM and garbage collector.We knew that JVM does not guarantee that when garbage collector will work.It does not sweeps memory always an object goes outside the scope.So upon recycling the object in heap first garbage collector checks for associated finalize() method before recycling any object.
You must have noticed the keyword protected mentioned before finalize it is put just to prevent access outside the class.
NOTE:-finalize() is only called just prior to garbage collection to if any resource was left to be released .It is advised that you should not use finalize() method just to be on a safer side.Your program must release the resources in program itself .Since garbage collectors run is not guaranteed so finalize() execution will also not be guaranteed.
IMPORTANT:-
1.Do not always rely on finalize() method.
2.Like C++ has destructors finalize is just approximates the functionality of destructor.
So when you do not need an object anymore do not forget to release the non-Java resource.finalize() method provides an automatic way of releasing such resources this mechanism is called finalization.
By using finalization, you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.To add a finalizer to your class ,what you need to do is just add a finalize() method to your class as follows:-
public class FinalizeTest{
protected void finalize(){
}
}
Inside this finalize() method you must specify the actions that are needed to be performed before an object is recycled.
How finalize() method works:-
Its important to note the concept of JVM and garbage collector.We knew that JVM does not guarantee that when garbage collector will work.It does not sweeps memory always an object goes outside the scope.So upon recycling the object in heap first garbage collector checks for associated finalize() method before recycling any object.
You must have noticed the keyword protected mentioned before finalize it is put just to prevent access outside the class.
NOTE:-finalize() is only called just prior to garbage collection to if any resource was left to be released .It is advised that you should not use finalize() method just to be on a safer side.Your program must release the resources in program itself .Since garbage collectors run is not guaranteed so finalize() execution will also not be guaranteed.
IMPORTANT:-
1.Do not always rely on finalize() method.
2.Like C++ has destructors finalize is just approximates the functionality of destructor.


















0 comments:
Post a Comment
Post a Comment