Performance Zone is brought to you in partnership with:

Peter is a DZone MVB and is not an employee of DZone and has posted 161 posts at DZone. You can read more from them at their website. View Full User Profile

Object Resurrection

12.19.2012
| 4340 views |
  • submit to reddit

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.

 

Published at DZone with permission of Peter Lawrey, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gonzalez Roberto replied on Wed, 2012/12/19 - 4:02am

Very curious.
What GC do you have used?
What JRE version?
Do you know if this behavior is specified in a JSR?

Thank you

Dapeng Liu replied on Wed, 2012/12/19 - 5:32am

can't think of any practical usage of this ... 

Peter Lawrey replied on Fri, 2012/12/21 - 11:23am in response to: Gonzalez Roberto

AFAIK, All GCs do this for Java 6 and 7.  I haven't tested Java 5.0 or previous.  It is something I have known about for many years but never seen myself.

Peter Lawrey replied on Fri, 2012/12/21 - 11:24am in response to: Dapeng Liu

 I think it's useful to know if someone has done something "interesting" or unofrtunate in finalisers.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.