Using Finalize method?

15 views
Skip to first unread message

Rajnish

unread,
Jun 29, 2009, 11:22:58 AM6/29/09
to Technical Discussion
Hi All

I am bit confused on how should I use finalize method to make the best
use out of it? After all if this method is in JAVA API there must be
some LOGIC to have this method.

Please comment on use of finalize?

Sandeep Kadyan

unread,
Jun 29, 2009, 12:30:27 PM6/29/09
to technical-...@googlegroups.com
Hey folks,

I have following opinion about the usage of finalize() method. I would say that we should never relay on finalize() method and do not use it.
The finalize() method was invented to resemble the destructor of C++ or to provide a chance to free up resources that cannot be freed automatically by an automatic storage manager.

Before the storage for an object is reclaimed by the garbage collector, the Java virtual machine will invoke the finalizer of that object. But it is not specified anywhere how sooner the finalize method will be invoked.except to say that it will happen before the storage for the object is reused. So there can be following point which impose various reasons to don't use the finalize() method.

1. Time not gurantted:We never know how sooner the finalize method will invoked after GC is about to going to relaim the memory.
2. It is possible that the this object( on which finalize() method is invoked) can put the reference for this object some where which means the object's memory should be reclaim by GC.
3. It is also not specified that which thread or threads (on multi-processor) will invoked the finalize(). If during clean-up, the thread block somewhere or required any use specified lock. it going to be BIG PROBLEM as we will never get to know what exaclty happen and why the memory is not reclaim even thought everything is fine (except the finalize())
4. The Java imposes no ordering on finalize method calls. the finalize() method may be called in any order, or even concurrently as specified in previous point.


Thanks,
Sandeep Kadyan
e-M@il:sandeep...@gmail.com
Skype IM: sandeep.kadyan
YIM: theka...@yahoo.com
Cell:-+91 99116 14500

Rajnish

unread,
Jun 29, 2009, 12:46:23 PM6/29/09
to Technical Discussion
Hi Sandeep

I am not clear on the second point. Could you please elaborate?

Rajnish

On Jun 29, 9:30 pm, Sandeep Kadyan <sandeep.kad...@gmail.com> wrote:
> Hey folks,
>
> I have following opinion about the usage of finalize() method. I would say
> that we should never relay on *finalize()* method and *do not *use it.
> The *finalize()* method was invented to resemble the destructor of C++ or to
> provide a chance to free up resources that cannot be freed automatically by
> an automatic storage manager.
>
> Before the storage for an object is reclaimed by the garbage collector, the
> Java virtual machine will invoke the finalizer* *of that object. But it is
> not specified anywhere how sooner the finalize method will be invoked.except
> to say that it will happen before the storage for the object is reused. So
> there can be following point which impose various reasons to don't use the *
> finalize*() method.
>
> 1. Time not gurantted:We never know how sooner the finalize method will
> invoked after GC is about to going to relaim the memory.
> 2. It is possible that the this object( on which *finalize*() method is
> invoked) can put the reference for this object some where which means the
> object's memory should be reclaim by GC.
> 3. It is also not specified that which thread or threads (on
> multi-processor) will invoked the *finalize()*. If during clean-up, the
> thread block somewhere or required any use specified lock. it going to be *BIG
> PROBLEM* as we will never get to know what exaclty happen and why the memory
> is not reclaim even thought everything is fine (except the *finalize()*)
> 4. The Java imposes no ordering on finalize method calls. the
> *finalize()*method may be called in any order, or even concurrently as
> specified in
> previous point.
>
> Thanks,
> Sandeep Kadyan
> e-M@il:sandeep.kad...@gmail.com
> Skype IM: sandeep.kadyan
> YIM: thekadi...@yahoo.com
> Cell:-+91 99116 14500
>
>
>
> On Mon, Jun 29, 2009 at 8:52 PM, Rajnish <rajnis...@gmail.com> wrote:
>
> > Hi All
>
> > I am bit confused on how should I use finalize method to make the best
> > use out of it? After all if this method is in JAVA API there must be
> > some LOGIC to have this method.
>
> > Please comment on use of finalize?- Hide quoted text -
>
> - Show quoted text -

Sandeep Kadyan

unread,
Jun 29, 2009, 1:10:03 PM6/29/09
to technical-...@googlegroups.com
Hi,

You can see following example to understand the second point. I have made one correction in the point. Please see.
import java.util.ArrayList;
import java.util.List;

public class TestClass {
    private int x;

    public TestClass(int x) {
        this.x = x;
    }

    @Override
    public String toString() {
        return String.valueOf(x);
    }

    @Override
    public void finalize() throws Throwable {
        System.out.println("TestClass.finalize()");
        super.finalize();
        // do some cleanup.
        SomeOtherClass.doCleanup(this);
    }

    public static class SomeOtherClass {
        private static List<TestClass> instances;
        static {
            instances = new ArrayList<TestClass>();
        }

        public static void doCleanup(TestClass instance) {
            // just an illustration
            instances.add(instance);
        }
    }

    public static void main(String[] args) throws Exception {
        TestClass clx = new TestClass(10);
        clx = null;// let GC to reclaim the memory
        System.out.println("Allocating huge object after one sec.");
        Thread.sleep(1000);
        byte[] bytes = new byte[10 * 1024 * 1024];
        System.out.println("Allocation Done. Now see all "
                + "the instances currently maintained");
        System.out.println(SomeOtherClass.instances);
//here we have the instance clx

       
    }
}


Sandeep Kadyan
e-M@il:sandeep...@gmail.com
Skype IM: sandeep.kadyan
YIM: theka...@yahoo.com
Cell:-+91 99116 14500


On Mon, Jun 29, 2009 at 10:16 PM, Rajnish <rajn...@gmail.com> wrote:

Hi Sandeep

I am not clear on the second point. Could you please elaborate?

Rajnish

On Jun 29, 9:30 pm, Sandeep Kadyan <sandeep.kad...@gmail.com> wrote:
> Hey folks,
>
> I have following opinion about the usage of finalize() method. I would say
> that we should never relay on *finalize()* method and *do not *use it.
> The *finalize()* method was invented to resemble the destructor of C++ or to
> provide a chance to free up resources that cannot be freed automatically by
> an automatic storage manager.
>
> Before the storage for an object is reclaimed by the garbage collector, the
> Java virtual machine will invoke the finalizer* *of that object. But it is
> not specified anywhere how sooner the finalize method will be invoked.except
> to say that it will happen before the storage for the object is reused. So
> there can be following point which impose various reasons to don't use the *
> finalize*() method.
>
> 1. Time not gurantted:We never know how sooner the finalize method will
> invoked after GC is about to going to relaim the memory.
> 2. It is possible that the this object( on which *finalize*() method is
> invoked) can put the reference for this object some where; which means the
> object's memory can not be reclaim by GC and finalize() will never call up again.

> 3. It is also not specified that which thread or threads (on
> multi-processor) will invoked the *finalize()*. If during clean-up, the
> thread block somewhere or required any use specified lock. it going to be *BIG
> PROBLEM* as we will never get to know what exaclty happen and why the memory
> is not reclaim even thought everything is fine (except the *finalize()*)
> 4. The Java imposes no ordering on finalize method calls. the
> *finalize()*method may be called in any order, or even concurrently as
> specified in
> previous point.
>
> Thanks,
> Sandeep Kadyan

Rajnish

unread,
Jul 2, 2009, 12:16:32 PM7/2/09
to Technical Discussion
Few more tips on using Finalize:

1. If you are creating a framework, then you can go for safer side and
free resources, inspite of waiting for the end user to do so.

2. Finalize are not called in chaining as the constructors do. we have
to call them manually.

3. We should write the code in finalize in try block and call
super.finalize in finally block so if there is an exception a call to
super is always made.
public void finalize() throws Throwable {
try {
// do some cleanup.
} finally {
super.finalize();
}
}

4. We can best use then to free the native resources in the framework
itself to be on the safer side because they are not collected by GC
even.

5. One important point is that if exception occurs in finalize and you
have not handled it then it is not propagated any more, not even to
JVM. The exception dies silently leaving no trace marks. That is why
try to write finalixe code in try catch finaly block.


-
Rajnish
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages