Assignments/Article By Gowsika Yogaraj

What is Garbage Collection in Java?

Student Assignment

Test Cases

Published on Oct 19, 2022

Garbage Collection in Java is a process by which the programs perform memory management automatically. The Garbage Collector (GC) finds the unused objects and deletes them to reclaim the memory. In Java, dynamic memory allocation of objects is achieved using the new operator that uses some memory and the memory remains allocated until there are references for the use of the object. When there are no references to an object, it is assumed to be no longer needed, and the memory, occupied by the object can be reclaimed. There is no explicit need to destroy an object as Java handles the de-allocation automatically. The technique that accomplishes this is known as Garbage Collection. Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have memory leaks. Garbage collection in Java happens automatically during the lifetime of the program, eliminating the need to de-allocate memory and thereby avoiding memory leaks.

How Does Garbage Collection in Java work?

Java garbage collection is an automatic process. Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting unused objects. An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused or unreferenced object is no longer referenced by any part of your program. So, the memory used by an unreferenced object can be reclaimed. The programmer does not need to mark objects to be deleted explicitly. The garbage collection implementation lives in the JVM.

Types of Activities in Java Garbage Collection

Two types of garbage collection activity usually happen in Java. These are:

Minor or incremental Garbage Collection: It is said to have occurred when unreachable objects in the young generation heap memory are removed.

Major or Full Garbage Collection: It is said to have occurred when the objects that survived the minor garbage collection are copied into the old generation or permanent generation heap memory are removed. When compared to the young generation, garbage collection happens less frequently in the old generation.

Important Concepts Related to Garbage Collection in Java

Unreachable objects: An object is said to be unreachable if it doesn’t contain any reference to it. Also, note that objects which are part of the island of isolation are also unreachable.

Unreachable objects: An object is said to be unreachable if it doesn’t contain any reference to it. Also, note that objects which are part of the island of isolation are also unreachable.

Example:

Integer i = new Integer (4);
// The new Integer object is reachable via the reference in ‘i’
i = null;
// The Integer object is no longer reachable.

Eligibility for garbage collectionAn object is said to be eligible for GC (garbage collection) if it is unreachable. After i = null, integer object 4 in the heap area is suitable for garbage collection in the above image.

Ways to make an object eligible for Garbage Collector:

Even though the programmer is not responsible for destroying useless objects, it is highly recommended to make an object unreachable (thus eligible for GC) if it is no longer required. There are generally four ways to make an object eligible for garbage collection.
  • Nullifying the reference variable
  • Re-assigning the reference variable
  • An object created inside the method
  • The Island of Isolation

Ways for requesting JVM to run Garbage Collector

Once we make an object eligible for garbage collection, it may not destroy immediately by the garbage collector. Whenever JVM runs the Garbage Collector program, then only the object will be destroyed. But when JVM runs Garbage Collector, we cannot expect it. We can also request JVM to run Garbage Collector. There are two ways to do it:

Using System.GC () method: System class contain static method GC () for requesting JVM to run Garbage Collector.

Using Runtime. get runtime().gc() method: Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its GC () method, we can request JVM to run Garbage Collector. There is no guarantee that any of the above two methods will run a Garbage Collector. The System. GC () is effectively equivalent to the call Runtime. Get Runtime (). GC ().

Finalization

Just before destroying an object, Garbage Collector calls finalize () method on the object to perform clean-up activities. Once finalize () method completes, Garbage Collector destroys that object. finalize () method is present in the Object class with the following prototype.

Protected void finalize () throws Throwable

Based on our requirement, we can override finalize () method for performing our clean-up activities like closing the connection from the database.
  • The finalize () method is called by Garbage Collector, not JVM. However, Garbage Collector is one of the modules of JVM.
  • The object class finalize () method has an empty implementation. Thus, it is recommended to override the finalize () method to dispose of system resources or perform other clean-ups.
  • The finalize () method is never invoked more than once for any object.
  • If an uncaught exception is thrown by the finalize () method, the exception is ignored, and the finalization of that object terminates.

Advantages of Garbage Collection in Java

  • It makes java memory-efficient because the garbage collector removes the unreferenced objects from heap memory.
  • It is automatically done by the garbage collector (a part of JVM), so we don’t need extra effort.

GC () method

The GC () method is used to invoke the garbage collector to perform clean-up processing. The GC () is found in the System and Runtime classes. public static void GC () {}

How can an object be unreferenced?

By nulling the reference

Employee e=new Employee();
e=null;

By assigning a reference to another

Employee e1=new Employee();
Employee e2=new Employee();
e1=e2;//now the first object referred by e1 is available for garbage collection

By anonymous object etc.
new Employee();

Example:

package uk.lset.accessmodifier.pack1;

public class TestGarbage1{
public void finalize () {
System.out.println(“object is garbage collected”);
}
public static void main (String args[])
{
  TestGarbage1 s1=new TestGarbage1();
  TestGarbage1 s2=new TestGarbage1();
  s1=null;
  s2=null;
  System.gc();
}
}

Output: object is garbage collected

Student Assignment

Follow Us

Resources

Presentations
Browse LSET presentations to understand interesting…

Explore Now


eBooks
Get complete guides to empower yourself academically…

Explore Now


Infographics
Learn about information technology and business…

Explore Now

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Beyond the Basics Advanced Techniques and Tools for Ethical Hacking Professionals

Beyond the Basics: Advanced Techniques and Tools for Ethical Hacking Professionals

Are you an ethical hacking professional looking to take your skills to the next level?...
Read More
Mastering Full Stack Python Development with Django A Comprehensive Guide

Mastering Full Stack Python Development with Django: A Comprehensive Guide

Python is a powerful programming language that has taken the world of web development by...
Read More
Mastering Machine Learning A Beginner's Guide to Python

Mastering Machine Learning: A Beginner’s Guide to Python

Welcome to the world of machine learning! With the ever-increasing demand for artificial intelligence and...
Read More