10 points on finalize method in Java – Tutorial Example

finalize method in java is a special method much like the main method in java. finalize() is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing connection if open etc. Main issue with finalize method in Java is it's not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example, an object may wait indefinitely after becoming eligible for garbage collection and before its finalize() method gets called. similarly even after finalize gets called it's not guaranteed it will be immediately collected. Because of above reason it makes no sense to finalize method for releasing critical resources or perform any time critical activity inside finalize. It may work in development in one JVM but may not work in other JVM. In this Java tutorial, we will see some important points about finalize method in Java, How to use finalize method, what to do and what not to do inside finalize in Java.  Btw, the best advice on finalize method is given by none other than Joshua Bloch on his time less classic book, Effective Java. Don't forget to read that after reading this article.

What is finalize method in Java – Tutorial Example

1) finalize() method is defined in java.lang.Object class, which means it available to all the classes for the sake of overriding. finalize method is defined as protected which leads to a popular core java question "Why finalize is declared protected instead of public"? well, I don't know the exact reason its falls in the same category of questions like why java doesn't support multiple inheritance which can only be answered accurately by designers of Java. any way making finalize protected looks good in terms of following rule of encapsulation which starts with least restrictive access modifier like private but making finalize private prevents it from being overridden in subclass as you can not override private methods, so making it protected is next obvious choice.

2) One of the most important points of finalize method is that it's not automatically chained like constructors. If you are overriding finalize method then it's your responsibility to call finalize() method of the superclass, if you forgot to call then finalize of super class will never be called. so it becomes critical to remember this and provide an opportunity to finalize of super class to perform cleanup. The best way to call super class finalize method is to call them in the finally block as shown in below example. This will guarantee that finalize of the parent class will be called in all condition except when JVM exits:

    protected void finalize() throws Throwable {
            System.out.println("Finalize of Sub Class");
            //release resources, perform cleanup ;
        }catch(Throwable t){
            throw t;
            System.out.println("Calling finalize of Super Class");

3) finalize method is called by garbage collection thread before collecting object and if not intended to be called like a normal method.

4) finalize gets called only once by GC thread if object revives itself from finalize method than finalize will not be called again.

5) Any Exception is thrown by finalize method is ignored by GC thread and it will not be propagated further, in fact, I doubt if you find any trace of it.

6) There is one way to increase the probability of running of finalize method by calling System.runFinalization() and
Runtime.getRuntime().runFinalization(). These methods put more effort that JVM call finalize() method of all object which are eligible for garbage collection and whose finalize has not yet called. It's not guaranteed, but JVM tries its best.

Alternative of finalize method for cleanup.

what is finalize method in Java – Tutorial ExampleSo far its seems we are suggesting not to use finalize method because of its non-guaranteed behavior but than what is alternative of releasing resource, performing cleanup because there is no destructor in Java. Having a method like close() or destroy() make much sense for releasing resources held by classes. In fact, JDK library follows this. if you look at java.io package which is a great example of acquiring system resource like file descriptor for opening file, offers close() method for opening stream and close() for closing it. In fact its one of the best practice to call the close  method from the finally block in java. Only caveat with this approach is its not automatic, client has to do the cleanup and if client forgot to do cleanup there are chances of resources getting leaked, which again suggest us that we could probably give another chance to finalize method. You will be pleased to know that Java 7 has added automatic resource management feature which takes care of closing all resource opened inside try block automatically, leaving no chance of manual release and leakage.

When to use finalize method in JavaIn the last paragraph, I pointed out that there are certain cases where overriding finalize make sense like an ultimate last attempt to cleanup the resource. If a Java class is made to held resource like input-output devices, JDBC connection then you should override finalize and call its close() method from finalize. though there is no guarantee that it will run and release the resource timely best part is we are not relying on it. It just another last attempt to release the resource which most likely have been already released due to the client calling the close() method. This technique is heavily used inside Java Development library. look at below example of finalize method from FileInputStream.java

protected void finalize() throws IOException {
        if ((fd != null) &&  (fd != FileDescriptor.in)) {

             * Finalize should not release the FileDescriptor if another
             * stream is still using it. If the user directly invokes
             * close() then the FileDescriptor is also released.
            try {
            } finally {


What not to do in finalize method in Java

trusting finalize method for releasing critical resource is biggest mistake java programmer can make. suppose instead of relying on close() method to release file descriptor, you rely on finalize to relapse it for you. Since there is no guaranteed when finalize method will run you could effectively lock hundreds of file-descriptor of earlier opened file or socket and there is high chance that your application will run out of file-descriptor and not able to open any new file. It's best to use finalize as the last attempt to do cleanup but never use finalize as a first or only attempt.

How finalize() works in Java

That's all on finalize method in Java. as you have seen there are quite a lot of specifics about finalize method which java programmer should remember before using finalize in java. In one liner don’t do time critical task on finalize method but use finalize with caution.

Related Java Articles and Tutorials
  • How SubString method work internally in Java? (answer)
  • How to parse XML file in Java using DOM parser? (solution)
  • How to convert Enum to String in Java with Example? (program)
  • 10 best practices to follow while writing code comments in Programming? (tips)
  • 10 Object oriented design principle Java programmer should know? (article)
  • 10 JVM options Java developer should know. (read more)
  • How to avoid deadlock in Java – Code Example (answer)

Further Reading
It's extremely rare to override Objects.finalize() method. Its not advised to override it but if you have to then first read and understand Effective Java, Item 7, "Avoid Finalizers" , very carefully, and then don't do it. 


Ralicy said...

I was looking information on how to call finalize method in java as I knew finalize is not chained like constructor. that seems buggy to me. anyway never knew this much information on java finalize method, thanks .

Anonymous said...

very informative post.. to learn other languages the tutorials available at http://goforexam.blogspot.com

Abhishek Goray said...

but can finalize method be overloaded?????

Anonymous said...

Actually there is an alternative mechanism in Java, but it is cumbersome to implement and quite difficult to do correctly.
The PhantomReference is positioned by Java as the preferred alternative to finalizers. It also solves some other issues on finalizers as from which context (thread).

sahu said...

Why finalize is declared protected instead of public? because it will only be called by JVM. it is protected, as the class use it, they will override it .

Kumaran said...


If if an object made reference during finalize() will that object get garbage collected or not ?

Rajkumar Singh said...

why finailize method is decalred as protected, the rest of the method in the object class are declared as public

manju said...

@RajKumar, even clone() method is declared as protected.

Anonymous said...

What's difference between finalize() method and System.gc() method ?

Shantinath Chaudhary said...

Hi, point 6 is very misleading-- "There is one way you can guarantee running of finalize method by calling System.runFinalization()". There seems no way to guarantee running of this method. I just tried it in a program, it doesn't run.
The Oracle reference doc too reads- "Calling this method suggests that the Java Virtual Machine expend effort toward running the finalize methods of objects that have been found to be discarded but whose finalize methods have not yet been run. When control returns from the method call, the Java Virtual Machine has made a best effort to complete all outstanding finalizations."
But Your blog is very good, and highly informative. Please modify the 6th point if you get sometime.

Javin Paul said...

@Shantinath, you are right, finalization is not guaranteed, it always work on best effort basis. Thanks for pointing it out, corrected now

Shantinath Chaudhary said...

@Javin: So humble of you to acknowledge it. You are doing great work writing these articles. These java articles on your blog are clearing Java doubts in such a simple, exemplary and conceptual way, which I am sure none other website would be doing. Your blog is really a great help for Java folks looking around to learn more, and for people appearing in interviews. Keep up the great work.

Andy said...

Very useful article. Talking about your first point, I think the next restrictive access modifier to 'Private' is 'Default' and not 'Protected'. Please let correct me if I'm wrong.

Post a Comment