Garbage Collection
Garbage Collection
Java garbage collection is the process by which Java programs perform automatic memory
management.
Garbage collector is best example of Daemon thread as it is always running in
background.
Main objective of Garbage Collector is to free heap memory by
destroying unreachable objects.
Once we made object eligible for garbage collection, it may not destroy
immediately by the garbage collector. Whenever JVM runs the Garbage
Collector program, then only the object will be destroyed. But when JVM
runs Garbage Collector, we can not expect.
We can also request JVM to run Garbage Collector. There are two ways
to do it :
1. Using System.gc() method : System class contain static
method gc() for requesting JVM to run Garbage Collector.
2. Using Runtime.getRuntime().gc() method : Runtime
class allows the application to interface with the JVM in which the
application is running. Hence by using its gc() method, we can
request JVM to run Garbage Collector.
Note :
1. There is no guarantee that any one of above two methods will definitely
run Garbage Collector.
2. The call System.gc() is effectively equivalent to the
call : Runtime.getRuntime().gc()
Finalization
Just before destroying an object, Garbage Collector
calls finalize() method on the object to perform cleanup activities.
Once finalize() method completes, Garbage Collector destroys that
object.
finalize() method is present in Object class with following prototype.
protected void finalize() throws Throwable
Garbage Collection in java
Note :
1. The finalize() method called by Garbage Collector not JVM. Although
Garbage Collector is one of the module of JVM.
2. Object class finalize() method has empty implementation, thus it is
recommended to override finalize() method to dispose of system
resources or to perform other cleanup.
3. The finalize() method is never invoked more than once for any given
object.
4. If an uncaught exception is thrown by the finalize() method, the
exception is ignored and finalization of that object terminates.
System.out.println("end of main");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
Output:
end of main
Garbage Collection in java
System.out.println("end main");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
System.out.println(10/0);
}
}
Output:
finalize method called
end main
Explanation :
When Garbage Collector calls finalize() method on an object,
it ignores all the exceptions raised in the method and program will
terminate normally.
Program 3 :
@Override
protected void finalize()
{
count++;
t = this; // line 38
}
Output:
finalize method called 1 times
Explanation :
After execution of line 12, t1 becomes eligible for garbage collection. So
when we call garbage collector at line 15, Garbage Collector will call
finalize() method on t1 before destroying it. But in finalize method, in
line 38, we are again referencing the same object by t, so after
execution of line 38,this object is no longer eligible for garbage
collection. Hence, Garbage Collector will not destroy the object.
Now again in line 21, we are making same object eligible for garbage
collection one more time. Here, we have to clear about one fact about
Garbage Collector i.e. it will call finalize() method on a particular object
exactly one time. Since on this object, finalize() method is already
Garbage Collection in java
line 8?
Answer :
1
Explanation :
By the time line 8 has executed, the only object without a reference is
the one generated i.e as a result of line 6. Remember that “Java is
strictly pass by value” so the reference variable t1 is not affected by the
m1() method. We can check it using finalize() method. The statement
“System.out.println(this.hashcode())” in finalize() method print the
object hashcode value on which finalize() method is called,and then just
compare the value with other objects hashcode values created in main
method.
https://java2blog.com/garbage-collection-java/