Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a "finalizer guardian" work in java?

How does a "finalizer guardian" [Effective Java , page 30] work ?

Have you used them? Did it solve any specific problem ?

like image 259
Vinoth Kumar C M Avatar asked Jul 29 '11 12:07

Vinoth Kumar C M


People also ask

What is finalizer method in java?

Overview. Finalize method in Java is an Object Class method that is used to perform cleanup activity before destroying any object. It is called by Garbage collector before destroying the object from memory. Finalize() method is called by default for every object before its deletion.

How does a finalizer work?

You can use finalizers to control garbage collection of resources by alerting controllers to perform specific cleanup tasks before deleting the target resource. Finalizers don't usually specify the code to execute. Instead, they are typically lists of keys on a specific resource similar to annotations.

Why finalizer () method is used?

The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed. The method is protected and therefore is accessible only through this class or through a derived class.

Why it is recommended to avoid using the finalize () method?

You shouldn't implement a Finalize method for managed objects because the garbage collector releases managed resources automatically. If a SafeHandle object is available that wraps your unmanaged resource, the recommended alternative is to implement the dispose pattern with a safe handle and not override Finalize.


1 Answers

It solves the problem of the sub-class forgetting to call the finalize method of the super-class. This pattern works by attaching an extra instance with overridden finalize to your super-class. This way, if the super-class goes out of scope, the attached instance would also go out of scope, which would trigger the execution of its finalize, which would in turn call the finalize of the enclosing class.

Here is a short snippet that showcases the guardian pattern in action:

public class Parent {

    public static void main(final String[] args) throws Exception {
        doIt();
        System.gc();
        Thread.sleep(5000); //  5 sec sleep
    }

    @SuppressWarnings("unused")
    private final Object guardian = new Object() {
        @Override protected void finalize() {
            doFinalize();
        }
    };

    private void doFinalize() {
        System.out.println("Finalize of class Parent");
    }

    public static void doIt() {
        Child c = new Child();
        System.out.println(c);
    }

}

class Child extends Parent {

    // Note, Child class does not call super.finalize() but the resources held by the
    // parent class will still get cleaned up, thanks to the guardian pattern
    @Override protected void finalize() {
        System.out.println("Finalize of class Child");
    }

}
like image 97
Sanjay T. Sharma Avatar answered Oct 06 '22 05:10

Sanjay T. Sharma