Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Garbage Collection
3.
How does Garbage Collection in Java Works?
4.
Advantages of Garbage Collection
5.
Example
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

Garbage collector

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Garbage collection in Java is the cycle by which Java programs perform programmed memory management. Java programs are compiled to bytecode to run on a Java Virtual Machine. When a Java program executes on the JVM, objects are made on the heap, a piece of memory devoted to the program. At last, a few objects will never again be required. The garbage authority tracks these unused objects and erases them to free up memory.

Also see, Duck Number in Java and Hashcode Method in Java

Garbage Collection

Garbage collection is an automatic process during which JVM checks the objects of the heap, whether they are still referenced, and releases the memory used by those objects that are no longer required.

Also see,  Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How does Garbage Collection in Java Works?

Garbage collection works using several garbage collection algorithms, e.g., Mark and Sweep. There are different methods of garbage collectors in Java to collect other areas of heap memory. Since JVM provides memory management, developers don’t care about cleaning the memory, only focusing on creating the objects. Cleaning is done by garbage collectors only when the objects have no solid live reference. 

Advantages of Garbage Collection

  • It is memory-efficient because it removes the unreferenced objects from heap memory.
  • The garbage collector automatically does it, so we don’t need to make the extra effort.

Example

public class JavaExample{   
  public static void main(String args[]){  
  JavaExample obj=new JavaExample();  
obj=null;  
JavaExample a = new JavaExample();
JavaExample b = new JavaExample();
b = a;
System.gc();  
  }  
  protected void finalize() throws Throwable
  {
        System.out.println("JVM performs garbage collection");
  }
}

Output:

JVM performs garbage collection
JVM performs garbage collection

In this code,  we have created two objects and then interchanged their values. Then we used an inbuilt function System.gc(). The purpose of this method is to run the garbage collector. This gives the suggestion that the JVM must collect garbage in order to make it memory-free. The finalize keyword here is used to clean up the processing which is collected by garbage. You can compile java code just by clicking here.

Frequently Asked Questions

  •  How is garbage collection handled in Java? 

All objects are allocated on the heap area managed by the JVM. Once an object is no longer required and is not used by the application code, the garbage collector removes it and frees the unused memory.

  •  What is the purpose of garbage collection in java, and when is it used?

Java applications obtain objects in memory as needed. The function of garbage collection (GC) in the Java virtual machine (JVM) is to determine what memory is no longer used by a Java application and recycle this memory for other uses.

Key Takeaways

In this blog, we have covered:

  • What is a garbage collector?
  • How does Garbage Collection work in Java?
  • Advantages of Garbage Collection in Java.
  • Very few examples of Garbage Collection.
  • Types of Garbage Collection.


If you want to explore more about garbage collectors, visit our Memory Management Garbage Collection website.

Previous article
Difference between Comparable and Comparator
Next article
Access Modifiers in Java
Live masterclass