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.
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.
The following are some of the important advantages of using Garbage collection:
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.
When a reference variable is assigned null, the object it was pointing to becomes unreferenced.
Syntax:
Here is the syntax:
When a reference is reassigned to another object, the previously referenced object becomes eligible for garbage collection.
Syntax:
Here is the syntax:
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:
The following methods are commonly used to interact with the garbage collection process:
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:
This method requests garbage collection through the Runtime class. It requests the JVM to run the garbage collector programmatically.
Syntax:
Here is the syntax:
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:
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.
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.
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.
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.
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.
We request you to subscribe our newsletter for upcoming updates.