Garbage Collection in Java

Last Updated : 20 Jan 2026

Garbage collection in Java is an automatic process that removes unused objects from memory to free heap space. In this chapter, you will learn how garbage collection works and why it is important for efficient memory management.

What is Garbage Collection in Java?

First understand, what is Garbage in Java?

In Java, garbage means unreferenced objects.

Garbage collection is an automatic memory management process in which the Java Virtual Machine (JVM) identifies and removes objects that are no longer in use. This helps free heap memory and prevents memory leaks that allows programs to run efficiently without manual memory management.

Advantages of Using Garbage Collection

The following are some of the important advantages of using Garbage collection:

  • Garbage collection automatically frees memory by removing unused objects that reduces the risk of memory leaks in applications and it eliminates the need for manual memory management.
  • Garbage collection allows developers to focus more on program logic rather than memory handling.

How Can an Object Be Unreferenced in Java?

An object becomes eligible for garbage collection when it is no longer referenced by any variable. There are several ways an object can be unreferenced.

1. By Nulling a Reference

When a reference variable is assigned null, the object it was pointing to becomes unreferenced.

Syntax:

Here is the syntax:

2. By Assigning a Reference to Another Object

When a reference is reassigned to another object, the previously referenced object becomes eligible for garbage collection.

Syntax:

Here is the syntax:

3. By Using an Anonymous Object

An object created without assigning it to any reference is called an anonymous object and is immediately eligible for garbage collection.

Syntax:

Here is the syntax:

Important Methods Used in Garbage Collection

The following methods are commonly used to interact with the garbage collection process:

1. System.gc() Method

This method is used to request the JVM to perform garbage collection. The request may or may not be honored by the JVM. It requests garbage collection to free unused memory.

Syntax:

Here is the syntax:

2. Runtime.getRuntime().gc() Method

This method requests garbage collection through the Runtime class. It requests the JVM to run the garbage collector programmatically.

Syntax:

Here is the syntax:

3. finalize() method

This method is invoked by the garbage collector before an object is removed from memory. It is used to perform cleanup operations before object destruction.

Syntax:

Here is the syntax:

Note: The Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup processing (destroying remaining objects).

Student Record Example Using Garbage Collection

Suppose you are developing a Student Record System. Student objects are created while the program is running. Once some student records are no longer needed, their memory should be released automatically using garbage collection.

Example Code

Here is the example code that demonstrates garbage collection using a student-based real-world scenario.

Output:

Student object is garbage collected
Student object is garbage collected

In this example, when student objects s1 and s2 are set to null, they become unreachable. The JVM garbage collector automatically removes these unused objects from memory, demonstrating the concept of garbage collection.

JVM Role in Garbage Collection

Garbage collection is completely managed by the Java Virtual Machine (JVM). The JVM automatically tracks object usage and removes objects that are no longer reachable, without any direct intervention from the programmer.

When Garbage Collection Runs

Garbage collection runs automatically when the JVM decides that memory needs to be freed. It usually occurs when heap memory is low or when the JVM requires more space for new objects. The exact time of execution depends on the JVM and system conditions.

The gc() vs Garbage Collection

Calling System.gc() or Runtime.getRuntime().gc() only requests garbage collection. The actual garbage collection process is controlled by the JVM, and there is no guarantee that it will run immediately.