What is Java Garbage collection?

What is Java Garbage collection?

In Java, we have JRE (Java Runtime Environment) that provides the implementation of automatic memory management. It has heap memory, that stores all the objects. Heap memory supports the reorganization or relocation of objects that reside in the memory, hence improving its efficiency, and no breakage of references is involved. By garbage, we mean the memory that was once used by the objects, but now, won’t be used or written ever.
Garbage collection is one of the most important features of Java language. In this article, we’re going to learn about Garbage collection, when it is needed, how to use it, and its advantages and disadvantages.

Garbage collection

In Java, the memory obtains the object as needed. When memory is no longer in use, we need to use some checker to recycle this memory, and that’s when Java’s Garbage collection comes into play. It resides in Java Virtual Machine (JVM). It automatically determines if there is unused memory that needs to be recycled and hence makes our work easy. We need not explicitly declare anything.

Objects in Java are mostly short-lived and ready to be reclaimed soon after their creation. The unreferenced objects that reside in the heap memory are automatically removed, this process makes the memory efficient. We as a user, are free from the task of memory deallocation.

Types of garbage

During the execution of the program, we encounter the following types of garbage –

  • Syntactic garbage – The object that leads to garbage is clear from the code itself.
  • Semantic garbage – It can only be determined at runtime that a memory used by an object is garbage.

Advantages

  • Since it removes the unreferenced objects that reside in the heap memory, it makes the memory efficient.
  • No extra effort is needed from our side, everything is done by the garbage collector itself.
  • It removes the Dangling pointer bugs. It is a type of bug in which objects still have references, even though the memory is freed.
  • It eliminates the Double free bugs. It occurs when there is an attempt to free up a piece of memory that has already been freed.
  • It reduces Memory leaks that can happen when the memory that is no longer referenced by the object is not freed up in the program.

Disadvantages

  • Sometimes it brings some runtime overheads that are not under the programmer’s control.
  • It can lead to performance problems for large-scale applications sometimes.

How to use garbage collection?

We can unreferenced the object by –

  • By nulling the reference that it is using
People p=new People();  
p=null;  Code language: Java (java)
  • By assigning its reference to another
People p1=new People();  
People p2=new People();  
p1=p2;//now the first object referred by p1 is available for garbage collection  Code language: Java (java)
  • By using an anonymous object
new People();Code language: Java (java)

finalize() method

Before the object is garbage collected, finalize() method is invoked each time. For cleanup processing, this method is used.
This method resides in the Object class and is defined as –

protected void finalize(){}  Code language: Java (java)

gc() method

In order to invoke a garbage collector, gc() method is used. It is found in the System and Runtime classes.

public static void gc(){}  Code language: Java (java)

Conclusion

In this article, we discussed what is garbage collector in Java, when it is needed, how it is used, and its pros and cons. When it comes to large applications, it becomes difficult to choose the right garbage collector that will optimize the performance. The JVM can always run the garbage collector, so you can ask it to run it anytime.

Thanks for reading!

Sharing is caring

Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far