finalize method Java
finalize() is a method of Object class which is called by the garbage collector automatically on an object when garbage collection determines that there are no more references to the object. Most of the time it invoked the method when heap runs on low memory.
1. The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
2. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded.
3. The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
4. The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked.
5. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
6. After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
7. The finalize method is never invoked more than once by a Java virtual machine for any given object.
8. Throwable is the Exception raised by this method.
NOTE: This information is taken from oracle official website.
Example: overriding the finalize method in our class:
class Demo
{
int x = 10;
int y = 20;
//overriding the finalize method
public void finalize() throws Throwable
{
System.out.println("Finalize method");
}}
//finalize method will get printed on the screen if finalize method and GC runs.
class Temp
{
public static void main(String[] s)
{
show();
}
static void show()
{
Demo d1 = new Demo();
display();
}
static void display()
{
Demo d2 = new Demo();
}}
The garbage collector runs after the finalize method. So if this method executed in the program then a message “finalize method” will get printed on the screen and the garbage collector will clear the unreachable objects from heap area.
Requesting JVM to invoke the Garbage collector explicitly:
As we already discussed that we cannot force JVM to invoke garbage collector and we can request only for this. So There are two methods to request JVM to invoke garbage collection:
· System.gc();
· Runtime.gc();
Example: Using System.gc() to invoke GC:
class Demo
{
int x =10;
int y =20;
public void finalize() throws Throwable
{
System.out.println("Finalized method executed");
}
}
class Temp
{
static void display()
{
Demo d2 = new Demo();
System.out.println("Display");
}
static void show()
{
Demo d1 = new Demo();
System.out.println("Show");
display();
}
public static void main(String[] args)
{
show();
System.gc(); //command to invoke GC
}
}
If GC runs successfully, we will see the following output:
Example: In this example we will create a lot of Objects and the JVM will have to invoke the Garbage collector automatically to clear memory.
class Demo
{
int x =10;
int y =20;
public void finalize() throws Throwable
{
System.out.println("finalize method and garbage collector running");
}
}
class Temp
{
public static void main(String[] s)
{
for(int i=0; i<100000; i++)
{
new Demo();
}
}}
In this program we created a lot of unreachable objects and as heap size is limited. So, the garbage collector will run each time it will feel heap running low on memory.
Output:
Some Important Questions:
Question: What is the difference between final, finally and finalize?
Answer:
1. “final” is a keyword in Java which is used for various purpose. Some important of them is to define a variable as final (constant), to stopinheritance(if we make a class final then it cannot be inherited) and to stop method overriding (if we declare any method as final then it cannot be overridden by child class).
2. “finally” is a block in exception handling which executes at any condition.
3. and “finalize” is a method in garbage collection.
Question: What kind of objects are free by the garbage collector automatically?
Answer: Only unreachable objects are cleared in Garbage collector.
Question: What is an unreachable object?
Answer: Those objects are called unreachable whose RID(reference ID) is lost from the program.
Question: If we call the finalize method explicitly then what will happen?
Answer: Nothing happens, function will run normally.
Question: Can you force JVM to start garbage collection process explicitly?
Answer: No, we cannot force. we can request only. If the JVM will find the heap size low on memory then it will start the garbage collection automatically.
Question: Can you get back the reference of an unreachable object?
Answer: Yes, we can do this if we can get object just before the garbage collector runs.
We can regain the reference of an unreachable object by catching the reference of object in “this” keyword.
class Demo
{
static Demo z;
int x;
int y;
public void finalize() throws Throwable
{
System.out.println("The End");
z = this;
}}
But this can be done only once. Second time JVM will destroy its reference without invoking finalize because the JVM follows mark and sweep algorithm.
No comments:
Post a Comment