Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good uses of the finalize() method [duplicate]

This is mostly out of curiosity.

I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ?

If you haven't encountered any what would you say a good usage would be ?

like image 449
Simeon Avatar asked Nov 18 '10 13:11

Simeon


People also ask

What is the purpose of using finalize method?

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.

What is the purpose of finalize () method in Java?

The Java finalize() method of Object class is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection to perform clean-up activity.

Why is it preferred to not use finalize for clean-up?

Finalizers are time-limited and the GC will terminate a finalizer if it blocks for too long - this is the primary reason to avoid it where possible.

What is true of the finalize () method How many times?

It is invoked only once during the execution of a program. Following are some notable points about the finalize method. Since this method belongs the Object class, which is the super class of all the classes in java, you can override it from any class.


1 Answers

If your Java object uses JNI to instruct native code to allocate native memory, you need to use finalize to make sure it gets freed.

like image 55
bmargulies Avatar answered Sep 23 '22 14:09

bmargulies