Garbage Collections:
—-------------------
In C++, Developers are responsible for creating objects and destroying
objects, so in C++ to destroy the objects we must use the Destructors.
In Java, developers are responsible for creating objects, Developers are not
responsible for destroying objects, because in Java applications, to destroy
objects Java has provided an internal tool called “Garbage Collector”.
In Java applications, when the Heap memory is full and when the objects are
dereferenced[The Objects without the reference] then that Objects are
destroyed by the Garbage Collector automatically.
In Java applications, we can destroy the objects by using Garbage Collector
explicitly.
If we want to destroy the Objects explicitly in Java applications we have to
use the following steps.
1. Make Eligible an object for the Garbage Collection.
2. Activate the Garbage Collector to destroy the Objects.
EX:
class A{
A(){
System.out.println("Object Creating.....");
}
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying....");
}
}
public class Main {
public static void main(String[] args)throws Throwable {
A a = new A();
a = null;
System.gc();
}
}
Object Creating.....
Object Destroying....
To make an object eligible for the Garbage collection we have to use the
following approaches.
1. Nullify the reference variable of an Object.
If any object is no longer required then we can make that object
eligible for Garbage Collection by assigning null value to all of its
reference variables.
EX:
class Student{
Student(){
System.out.println("Object Creating.....");
}
public void finalize(){
System.out.println("Object Destroying....");
}
}
public class Main {
public static void main(String[] args){
Student s1 = new Student();
Student s2 = new Student();
s1 = null;
System.gc();
s2 = null;
System.gc();
}
}
2. Re-assign the reference variable:
If we reassign a new value to the reference variable which has already
a reference value then the old object which is referenced by the
reference variable is eligible for the garbage Collection.
EX:
class Student{
Student(){
System.out.println("Object
Creating.....");
}
public void finalize(){
System.out.println("Object
Destroying....");
}
}
public class Main {
public static void main(String[] args){
Student s1 = new Student();
s1 = new Student();
System.gc();
}
}
Object Creating.....
Object Creating.....
Object Destroying....
3. Objects Created inside the Methods:
If we create an object inside the methods then the Object will be
created when we access that method, when the method execution is
completed automatically the generated object is eligible for the
Garbage Collection.
EX:
class Student{
Student(){
System.out.println("Object Creating.....");
}
public void finalize(){
System.out.println("Object Destroying....");
}
}
class A{
void m1(){
Student std = new Student();
}
}
public class Main {
public static void main(String[] args){
A a = new A();
a.m1();
System.gc();
}
}
Object Creating.....
Object Destroying....
Requesting Garbage Collector to destroy the Objects:
—-----------------------------------------------------
IN general, in the java applications, when we make an object eligible
for the Garbage Collection then we have to request the Garbage
collector to destroy the objects.
In Java applications, when we request a JVM to activate the garbage
Collector, there JVM may accept or may not accept the request is
totally dependent on the internals of the JVM, in most of the cases
JVM will accept the request.
To request the JVM to activate the Garbage Collector we will use the
following approaches.
1. By using System class.
System.gc();
2. By using Runtime class.
Runtime rt = Runtime.getRuntime();
rt.gc();
EX:
class A{
A(){
System.out.println("Object Creating.....");
}
public void finalize(){
System.out.println("Object
Destroying.....");
}
}
public class Main {
public static void main(String[] args){
A a = new A();
a = null;
Runtime rt = Runtime.getRuntime();
rt.gc();
}
}
Q)If we access the finalize() method explicitly in java applications then is
it possible for the garbage Collector to perform garbage Collection over the
Object?
—---------------------------------------------------------------------------
-
Ans:
—---
No, Garbage Collector will perform garbage Collection when the Object are
eligible for the Garbage Collection, not because of accessing finalize()
method explicitly, if we access the finalize() method explicitly then JVM
will access that finalize() like a normal java method.
class A{
A(){
System.out.println("Object Creating.....");
}
public void finalize(){
System.out.println("Object
Destroying.....");
}
void m1(){
System.out.println("m1-A");
}
}
public class Main {
public static void main(String[] args){
A a = new A();
a.finalize();
a.m1();
}
}
Q)What is Memory Leak?
—----------------------
Ans:
—---
If any Object exists in a Java application without utilization and which is
not eligible for the Garbage Collection then that Object is called a Memory
Leak.
If we get More memory leaks then the garbage Collector is not also not doing
anything, where the java application will be crushed out, in this case we
will get OutOfMemoryException.