Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Garbage Collection

• As we know that in Java we have a Dynamic Memory Allocation concept which helps us to allocate memory into the Heap area on demand, like in the case of Array or Objects.
• Generally we use new operator to allocate dynamic memory in heap.
• In C and C++ programming language by using malloc() and calloc() we can allocate some memory inside heap but whenever it is required you can reclaim the memory allocated by using free() or delete() function.
• In Java Programming language we do not need to write any code to clean up the Heap memory allocation, the task is carried out by JVM automatically with the help of Garbage Collection feature present in Java.
• Garbage Collection is a process to clean up the unused or unreferenced memory allocation from the Heap.

Now we will discuss how JVM can identify an unused or unreferenced object using few examples.

            
class JTC{
	public static void main(String arg[]){
		A a1 = new A();
		A a2 = new A();
		a1 = null; // line - 7
	}
}
class A{
}

In the above example as you can see we have two classes A and JTC, in JTC class we can see that we are creating two different objects of A class A a1 = new A() and A a2 = new A(), it means in Heap we get two objects and their reference gets stored into the corresponding reference variables a1 and a2 respectively. In Line-7 we have assigned null value into a1, it means that after line-7 the first object of A class A a1 = new A() would now become eligible for Garbage Collection; after garbage collection process gets completed there will remain no reference for that object and we won’t be able to use that object further in the program.

            
class A{
	static void m1(){
		A a1 = new A();
		A a2 = new A();
	}
	public static void main(String arg[]){
		m1();
	}
}

In this example we have a class which is A and inside the A class we have a method void m1(), inside the void m1() we are creating two objects of A class A a1 = new A() and A a2 = new A(), because A a1 and A a2 have been declared inside the m1() method so these are the local variables of void m1() and their memory allocation is inside the stack frame of m1() method local context itself. Both objects get their memory inside heap area.
As we know that a local context is allocated memory at the time of invocation and after the execution corresponding stack frame is automatically released from the stack area. So, as we can see that we are invoking the m1() method from main() method . Once execution of m1() method gets completed, both the objects of A class in heap area will be unreferenced and further we won’t be able to reuse those objects. Hence both the objects will become eligible for Garbage Collection.

• JVM starts the Garbage Collection process whenever redundancies in allocated memory arise. When the memory to create a new resource or object is no more sufficient then Garbage Collection process is invoked by JVM.
• JVM tasks include setting in motion the Garbage Collection process.

When any memory problem is detected by JVM inside the Heap Area than JVM follows the below mentioned steps: -

1. First of all, the JVM issues the process for the listing of unused or unreferenced objects or resources in Heap.
2. When an Object is eligible for Garbage Collection process then JVM does not reclaim that object immediately, when JVM gets notified of insufficiency of memory in Heap area then it starts the Garbage Collection process using gc() method of java.lang.System class.

a. public static void gc();

Access Modifier: - public
Types of Member: - Static Method
Return Type: - void
Method Name: - gc()
Functionality: - System.gc() method internally calls Runtime.getRuntime().gc() method. This suggests that the Java Virtual Machine applies tools towards recycling unused objects in order to make the memory they currently occupy available for quick reuse. When the control returns from the method call, it implies that the Java Virtual Machine has instigated all available tools to reclaim space from all discarded objects.

b. public static void runFinalization();

Access Modifier: - public
Types of Member: - Static Method
Return Type: - void
Method Name: - runFinalization()
Functionality: - System.runFinalization() method internally calls Runtime.getRuntime().runFinalization() method which suggests that the Java Virtual Machine applies its tools towards running the finalization methods of objects that have been found to have never been run and yet have been discarded. When the control returns from the method call, it implies that the Java Virtual Machine has instigated all available tools to complete all outstanding finalizations.

3. We can explicitly call the System.gc() to start the Garbage collection process but there is no guarantee that the Garbage collection process will start instantly, the starting time depends upon the CPU scheduler.