views:

570

answers:

6

I need to find when finalized method called in the JVM. I Created a test Class which write into file when finalized method called by Overriding the protected finalize method It is not executing. Can anybody tell me the reason why it is not executing?? Thanks in Advance

+10  A: 

The finalize method is called when an object is about to get garbage collected. That can be at any time after it has become eligible for garbage collection.

Note that it's entirely possible that an object never gets garbage collected (and thus finalize is never called). This can happen when the object never becomes eligible for gc (because it's reachable through the entire lifetime of the JVM) or when no garbage collection actually runs between the time the object become eligible and the time the JVM stops running (this often occurs when simple test programs are written).

There are ways to tell the JVM to run finalize on objects that it wasn't called on yet, but using them isn't a good idea either (the guarantees of that method aren't very strong either).

If you rely on finalize for the correct operation of your application, then you're doing something wrong. finalize should only be used for cleanup of (usually non-Java) resources. And that's exactly because the JVM doesn't guarantee that finalize is ever called on any object.

Joachim Sauer
I accepted your Quotes absolutely true. Otherwise I need to increase the Life time of the JVM until my Object get destroyed. Is there any way to do so???
Rajesh Kumar J
@Rajesh - you should really not be relying on finalizers. There should be a better way to do what you are trying to do.
Stephen C
Sir kindly look at my another question i mentioned the Program
Rajesh Kumar J
Which one? You have a bunch =) (Also, I concur. Don't use finalizers...if you're relying on them, then your fundamental design is flawed. Wether it's a design weakness or a strength, Java doesn't have C++'s destructors).
mikek
A: 

The finalize method will be called after the GC detects that the object is no longer reachable, and before it actually reclaims the memory used by the object.

If the object never becomes unreachable, or the GC doesn't run then finalize may never be called. And it may take more than one GC cycle before finalization actually occurs. (And indeed, the JVM spec allows a JVM to never run finalizers ... provided that it doesn't reclaim the space used by the objects.)

The upshot is that it is unwise to rely on finalize to do things that have to be done in a definite time-frame. It is "best practice" not to use them at all. There should be a better (i.e. more reliable) way to do whatever it is you are trying to do in the finalizer method.

Stephen C
+5  A: 

The Java finalize() method is not a destructor and should not be used to handle logic that your application depends on. The Java spec states there is no guarantee that the finalize method is called at all during the livetime of the application.

What you problably want is a combination of finally and a cleanup method, as in:

MyClass myObj;

try {
    myObj = new MyClass();

    // ...
} finally {

    if (null != myObj) {
        myObj.cleanup();
    }
}
rsp
You probably mean "destructor", not "deconstructor" ;-)
Helper Method
@Helper, oops good find! fixed :-)
rsp
+2  A: 

In general it's best not to rely on finalize() to do any cleaning up etc.

According to the Javadoc (which it would be worth reading), it is called:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object

As Joachim pointed out, this may never happen in the life of a program if the object is always accessible.

Also, the garbage collector is not guaranteed to run at any specific time. In general, what I'm trying to say is finalize() is probably not the best method to use in general unless there's something specific you need it for.

Phill Sacre
A: 
protected void finalize() throws Throwable {}
  • every class inherits the finalize() method from java.lang.Object
  • the method is called by the garbage collector when it determines no more references to the object exist
  • the Object finalize method performs no actions but it may be overridden by any class
  • normally it should be overridden to clean-up non-Java resources ie closing a file
  • if overridding finalize() it is good programming practice to use a try-catch-finally statement and to always call super.finalize(). This is a safety measure to ensure you do not inadvertently miss closing a resource used by the objects calling class

    protected void finalize() throws Throwable {
         try {
             close();        // close open files
         } finally {
             super.finalize();
         }
     }
    
  • any exception thrown by finalize() during garbage collection halts the finalization but is otherwise ignored

  • finalize() is never run more than once on any object

quoted from: http://www.janeg.ca/scjp/gc/finalize.html

You could also check this article:

XpiritO
A: 

finalize method is not guaranteed.This method is called when the object becomes for GC.There are many situations where the objects may not be garbage collected.

giri