Finalize method in java
when constructor initializes an object,the mechanism is called initialization.Similarly java supports one more method just opposite to this,called finalization. Java garbage collector frees up memory used by the objects.But objects may hold other non objects (not created by Java new operator) resources(like file descriptor,window system fonts etc).Garbage collector can not free up these items as they as not created by new operator. To reclaim such memories,finalize() method is used. Finalize () method needs to define explicitly.
Many a times programmers forget to cleanup memory.Nobody wants to clean up the small chunk of memory for int or character like this approach. To free up the memory java came up with a concept called Garbage collector to reclaim the memory of objects those which are no longer in use. But garbage collector can only reclaim memory for objects which are created by ‘new’ operator. If some program is occupying memory using some other way,Garbage collector won’t be able to reclaim those memories. To handle such condition, Java has provided a method called finalize().This method can be defined for any class.
How it works?
When Garbage collector is ready to release the storage of the object,it calls finalize() method first.And only the next Garbage collection pass will reclaim the memory for the object.We can override finalize() method by redefine the method.
We have to understand the below points while working with Garbage Collector:
- Garbage collection is not object destruction. As java has no concept for destructor for clean up memory. It just ensures if there is some activity that must be performed before it reclaim the memory. We must perform that activity by ourselves.We can create our own methods to cleanup the memory.
- All objects may not get garbage collected.It may so happen that our program never face the memory shortage issue(may be the code is too small and the available memory is too high).In that case garbage collector may reclaim the memory.So after termination of the program the memory is returned to the Operating system.Garbage collection process has some overhead,in the above way we never have to incur the expense.
- Garbage collection is not only about memory reclaim. If we use as synonym of memory release the finalize has to be same.
The role of Finalize():
Now if we create some other objects in our code and call finalize() method,will they be released? No it won’t. Garbage collector will take care of the release of other objects in the memory,irrespective of how the objects are created. Since everything in java are object,in that case where we will define finalize() method?
Well,Java supports native methods(C,C++ etc).In these native languages there are ways to allocate memories like calloc(),malloc().Unless we call free() method,the storage will never be released,causing memory leak in java code via native methods.We can define the C language free() method in the other function where all our functions were defined.It is a best practice to write free() method inside finalize() method.Java does not allow us to create local objects rather all objects should be created using new operator.