Apr 2, 2012

Finalization in Java,How to run finalize() method


1) We know that the finalize() method is run before an object is finally garbage collected.

2) The developer doesn't have any control over when the JVM will run the finalize method for the objects not required by the program.

3) The following program shows how one can suggest the JVM to run the finalize method but this suggestion is not gauranteed to be run.


public class MyTest{

public static void main(String[] args) {
MyTest t = new MyTest();
t = null;
System.runFinalization();
}
}


4) One can override the finalize method in the class and check when it is being invoked and hence see if the suggestion to run finalize method was accepted by Java Virtual Machine.

No comments:

Post a Comment