Java Object resurrection

Overview

After an object which overrides finalize() is collected it is added to a finalization queue to be cleaned up after calling the finalize() method of each object.  By what happens if you resurrect the object?
 
 
 
 
 
 

When is finalize called?

The finalize method is called by a single threaded system task which calls this method for each object which has been collected. Note: the nodes in the finalization queue are objects also have finalize() methods notionally. Objects cannot be cleaned up until the GC after they have been finalized.

Most objects (including the node in the finalization queue) don’t overriden finalize() and so the GC is smart enough to detect this and not add them to the queue. These obejcts can be cleaned up immediately. If you override the method, even with an empty one, it makes a difference.

What about resurrected objects?

In the finalize() method, you can resurrect the object by giving making something point to it. e.g. a static collection.  This object can no longer be collected by a GC (until it is discarded again)  So what happens then?

The object has been flags as being finalized once and is not finalized repeatedly.

static final List ZOMBIES = new ArrayList<>();
    static class Zombies {
        private int num;
        public Zombies(int num) {
            this.num = num;
        }
        @Override
        protected void finalize() throws Throwable {
            System.out.println("Resurrect " + num);
            ZOMBIES.add(this);
        }
        @Override
        public String toString() {
            return "Zombies{" + "num=" + num + '}';
        }
    }
    public static void main(String... args) throws InterruptedException {
        for (int i = 0; i < 3; i++)
            ZOMBIES.add(new Zombies(i));
        for (int j = 0; j < 5; j++) {
            System.out.println("Zombies: " + ZOMBIES);
            ZOMBIES.clear();
            System.gc();
            Thread.sleep(100);
        }
    }

prints

Zombies: [Zombies{num=0}, Zombies{num=1}, Zombies{num=2}]
Resurrect 2
Resurrect 1
Resurrect 0
Zombies: [Zombies{num=2}, Zombies{num=1}, Zombies{num=0}]
Zombies: []
Zombies: []
Zombies: []

In this example, the Zombies are added once to the collection and resurrected once by the finalize method. When they are collected a second time, they have been flagged as finalized and not queued again.

Conclusion

While it’s a good idea to avoid using finalize(), it is a small comfort to know it will only be called once it the object is resurrected.
 

Reference: Java Object resurrection from our JCG partner Peter Lawrey at the Vanilla Java blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


+ 1 = seven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books