Let’s learn garbage collection in java.
Garbage collection in java
Garbage collection is the process of deleting unreferenced objects in programs automatically by Garbage Collector(GC).

Garbage collector finds unused/unreferenced objects from heap memory and deletes them to reclaim memory.
In C/C++ programmer is responsible to manage memory. That is programmer is responsible for both creation and destruction of objects.
Generally programmer create object and neglects the destruction of useless objects. Due to this at certain point there may be no sufficient memory for creation of new objects and entire application will be down with insufficient memory.
Hence OutOfMemoryError is very common in languages like C++.
But in Java, programmer is responsible for creating objects and he is not responsible for destruction of objects.
Destruction of objects is handled by Garbage Collector. The main objective of garbage collector is to destroy useless objects and free up heap memory.
Garbage collector is an example for Daemon Thread which runs always in the background.
When is an object eligible for garbage collection?
An object is said to be eligible for garbage collection only if it does not contain any reference.
Ways to make an object eligible for garbage collection
Below are few ways,
Nullifying reference variable – if an object is no longer required then assign “null”.
Student obj = new Student();
obj = null;
Re-assigning reference variable – if an object is no longer required then re-assign reference to other object then old object will be automatically eligible for garbage collection.
Student obj1 = new Student();
Student obj2 = new Student();
obj1 = new Student(); // obj1 is eligible for garbage collection
obj2 = obj1; // obj1 and obj2 is eligible for garbage collection
Objects created inside a method
Objects created inside a method are by default eligible for garbage collection.
class Demo { public static void main(String[] args) { display(); } public static void display() { Employee emp1 = new Employee(); Employee emp2 = new Employee(); } }
After making object eligible for garbage collection, it may not destroy immediately by garbage collector. Below are ways to request java virtual machine to run garbage collector. They are,
- By using System class gc() method
- By using Runtime class
gc() method present in System class is static method. This method requests JVM to run garbage collector.
Runtime class is present in java.lang package. Runtime class is a singleton class. Here we can create runtime object by using getRuntime() method.
Runtime r = Runtime.getRuntime();
gc() method present in Runtime class is instance method.
NOTE:
With respect to performance it is recommended to use Runtime.getRuntime().gc() method when compared to System class gc() method.
Because internally System.gc() method calls Runtime class gc() method.
Advantages of garbage collection:
- Garbage collection in java happens automatically, since java relieves you from additional burden of releasing used memory. Hence making java memory efficient.
- Garbage collection ensures program integrity.
- We don’t have to write extra code since garbage collector is the part of JVM.
Also read – preface to java virtual machine and architecture