The Garbage Collection feature of the java language is possibly its best feature. Thanks to garbage collection, programmers do not have to worry about memory allocation and dellocation since it automatically frees up memory for further use. It removes unused objects. It recognises objects that have not been referenced, and seeing as they are not in use, it removes them.
In C language, we can use malloc() or dealloc() to allocate the memory required. You can also clean that memory using these functions.
In C++ language, we can use new operators to allocate the memory and delete operator to clean the memory which is allocated by new operator.
In java language, we can allocate the memory using new operator. But we don't have any process of cleaning memory in java.
In java, automatic memory cleaning process was designed to reduce the developer’s burden, which is called garbage collection.
JVM is responsible to detect unused objects time to time.
Following are some of the criteria JVM uses to detect unused objects:
When we assign null to the reference variable, then object referred by that reference variable is not referred or unused and is eligible for garbage collection Example:
Hello h=new Hello ();
h=null;
When you assign one reference variable to another reference variable, then the object referred by the reference variable which is on the left side of the assignment operator is not referred and is eligible for GC
Hello h1=new Hello();
Hello h2=new Hello();
h1=h2;
When the reference variable is out of scope, then the object referred by that reference variable is not referred or unused and is eligible for GC.
JVM uses the above mentioned said technique and identifies the unused object.When JVM detects a memory shortage problem, it invokes GC by handing over list of unused objects.
GC is a thread, which is mainly responsible for cleaning the memory of unused objects.
Sometimes unused objects may be refused by GC for cleaning the memory because they may hold the references to same resources like JDBC database connection, IO stream connection, printer connection, etc.
When you release the resources which are referred by some unused objects, then you can clean the memory of those unused objects without any trouble.
JVM invokes runFinalization() method of System class which is responsible for invoking Finalize() methods on all unused objects.
JVM works as following algorithm.
Detecting unused objects.
Prepares a list of unused objects
When memory shortage problem is detected, the JVM will do the following
runFinalization()
method
System.runFinalization();
Runtime.runfinalization();
gc()
method
System.gc();
Runtime.gc();
Cleans the memory of unused objects
runFinalization()
:- invokes the finalize() method on all unused objects finalize() method is of object class you need to override.
That is, override finalize () method in your class when you are using any resources in your class and write the resources cleanup code inside the finalize() method.
class Hello {
connection con=null;
---
void m1();
---
}
void m2() {
connection con = ---------
}
public void finalize() {
con.close();
}
class Hi {
---
}
package com.javabykiran.GC;
class Hi {
public void finalize() {
System.out.println("Hai-finalize()");
}
}
package com.javabykiran.GC;
class A {
void show() {
Hello h1 = new Hello();
h1.m1();
System.runFinalization();
System.gc();
Hello h2 = new Hello();
h2.m1();
System.runFinalization();
System.gc();
}
public void finalize() {
System.out.println("A-finalize()");
}
}
package com.javabykiran.GC;
class Hello {
void m1() {
System.out.println("m1-begin");
Hi h1 = new Hi();
Hi h2 = new Hi();
Hi h3 = new Hi();
System.out.println("m2-begin");
System.runFinalization();
System.gc();
Hi h2 = new Hi();
System.out.println("m2-end");
}
public void finalize() {
System.out.println("Hello-Finalize()");
}
}
package com.javabykiran.GC;
class lab3 {
public static void main(String[] args) {
A obj = new A();
obj.show();
System.runFinalization();
System.gc();
}
}
Output:
m1-begin
m2-begin
m2-end
m1-begin
Hai-finalize()
m2-begin
Hai-finalize()
Hai-finalize()
Hai-finalize()
m2-end
Hai-finalize()
Hai-finalize()
Hai-finalize()
Hai-finalize()
Hello-Finalize()
Hello-Finalize()