Garbage Collection in Java

The process by which Java performs automatic memory management is called garbage collection in Java. As long as the object is present on java virtual machine, it is considered to be alive. Once it is no longer found in the application code, it is removed by the JVM, and the memory space is made free. As a result, it is claimed to be an unused memory. The whole process is done during the run time of the code. JVM does this by converting the byte code into machine language.

Read More Java Tutorials

We use free() function to perform the above process in C language, delete() in C++ language, but java performs this function automatically.

Features of Garbage collection in Java

  • Garbage collection frees the unused space in the memory.
  • It destroys the unwanted objects automatically, so that wanted space can be reclaimed.
  • JVM recollects the heap space from the objects.
  • finalize() method is run to perform all sorts of required cleanups and JVM removes any object from the memory.
  • There are methods such as System.gc() and Runtime.gc() by which users can also send a request for memory cleanup. However, it is not certain that cleanup will happen or not.
  • A user cannot force a garbage collection. The collector itself checks for the heap size and performs the required action.

Example

When the object is unreachable: When an object doesn’t contain any reference to it.

DeveloperHelps obj = new DeveloperHelps();
obj = null; 

Here, the object of the class points towards the reference object. It means the object is no longer pointing to the object of the class and the value is null. So, it makes Developerhelps object unreachable. At this point, the garbage collector will work automatically and will free the unused space.

When we copy one reference to the other

DeveloperHelps obj = new DeveloperHelps();
DeveloperHelps obj2 = new DeveloperHelps();

ooj2 = obj; 

Here, obj2 is assigning towards the reference object. This means the actual object pointed by obj2 is unreachable which will be identified by garbage collector. It will thus free the space by putting it in garbage collection.

Below is an example of how garbage collection works along with the functions discussed in the Section 1:

public class Developerhelps{
public void finalize(){
System.out.println("JVM carries out the garbage collection process");
}
public static void main(String []args)
{
Developerhelps x =new Developerhelps();
Developerhelps y = new Developerhelps();
x= null;
y= null;
System.gc();
}
}


The output of the above program will be:

JVM carries out the garbage collection process

How does garbage collection work in Java?

Garbage collection in Java can be handled in two ways:

  • One method is by using gc() method through the system. System class in Java contains a method called gc() which helps in requesting Java Virtual Machine to call for the garbage collector. Later the garbage collector finds unwanted objects through gc() and deletes them from the memory
  • Another method for garbage collection in java is by using getRuntime() method. The class will help in building an interface between the application and Java Virtual Machine in which the application will be present and currently running. It returns the output as a runtime object. It is always the final class in java.

Summary for Garbage collection in Java:

To carry out the above process, there are 4 main criteria’s which make the object eligible for garbage collection:

  • By giving null value to the reference variable
  • Giving a newly assigned value to the reference variable
  • By creating the object inside a method
  • Island of isolation: It means a group of objects which are referring to each other but in real they are not referenced by any object with a definite value. It also means a state where there’s no possibility to call an object.

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!