Java Memory Model Part -2 | Java Garbage Collections

Sumit Chaudhari
4 min readApr 30, 2023

--

For this 3 part series we are going to discuss the second part Java Garbage Collections. If you haven’t read the JVM memory structure (Part-1), I do recommend to read it first.

In any programming language Garbage collection is the essential part that manages memory automatically. In Java, Garbage Collection (GC) plays a crucial role in memory management. It helps free up memory that is no longer in use by the application, ensuring that the system can continue to run efficiently without running out of memory. In this article, we will discuss the fundamentals of Java garbage collection and how it works.

Garbage Collection is the process to free up the runtime unused memory.

What is Garbage Collection?

Garbage Collection is the process of freeing up memory that is no longer in use by the application. In Java, this process is automatic and managed by the JVM (Java Virtual Machine). The JVM monitors the memory usage of the application and performs garbage collection as needed to free up memory.

Java programmes compiles to byte codes which runs on the JVM. when java programmes runs on JVM objects are created on the heap (A dedicated memory area for the programme). when these objects are no longer needed we need to destroy these objects. Garbage Collector finds these objects and destroys them.

The primary goal of garbage collection is to reclaim memory that is no longer being used by the application. This can happen when objects are no longer needed, or when the application has finished using a particular piece of memory. Garbage collection ensures that the memory is released back to the system for reuse by other applications.

How does Garbage Collection Work?

In Java, garbage collection works by periodically scanning the heap, which is the region of memory where objects are allocated, to identify objects that are no longer in use by the application. The heap is divided into different regions, and each region is managed by a different garbage collector. When an object is no longer needed, it is marked as eligible for garbage collection.

Java has different garbage collectors, each designed for specific use cases. The three most common garbage collectors in Java are:

  1. Serial Garbage Collector: It is a single-threaded collector that is best suited for small applications with limited memory requirements.
  2. Parallel Garbage Collector: This collector uses multiple threads to scan the heap and is best suited for large applications that require high throughput.
  3. Concurrent Mark Sweep (CMS) Garbage Collector: This collector uses multiple threads to scan the heap concurrently with the application’s execution, minimising the application’s downtime.
  4. G1 (Garbage First): It is newest garbage collector in Java and intended as a replacement for CMS. It is parallel and concurrent, like CMS. However, it works quite differently under the hood than older garbage collectors.

When the garbage collector identifies an object that is eligible for collection, it is removed from the heap, and the memory is returned to the system. The garbage collector also defragments the heap, compacting the memory and freeing up more space for the application.

Garbage Collector is the best example of Daemon Thread which always runs in the background. Follow me to get the updates for the upcoming articles on Daemon Thread.

Best Practices for Garbage Collection:

While garbage collection is automatic in Java, there are best practices that developers should follow to ensure efficient memory management. These include:

  1. Creating Objects Carefully: Developers should be mindful of creating objects only when needed and avoiding creating unnecessary objects, which can lead to increased memory usage and slower application performance.
HelloWorld hi = new HelloWorld("Sumit");
hi -> "Sumit"; //hi is pointing to Sumit in heap memory
hi = null;
hi -> null; //now hi pointing to null so Sumit is not reachable so
//it should be garbage collecte

2. Avoiding Object Finalisation: Finalisation is the process of cleaning up an object when it is no longer needed. However, finalisation can significantly impact performance, and developers should avoid using it unless necessary.

We can override finalize() method for performing our cleanup activities like closing connection from the database depending on our requirements.

3. Using the Right Garbage Collector: Choosing the right garbage collector for the application is crucial for efficient memory management. Developers should analyse the application’s memory requirements and choose the appropriate garbage collector accordingly.

When Garbage Collection Triggers in the system ?

  1. When there is no more memory remains in the heap for newly created objects, JVM triggers the Garbage Collectors to free up memory.
  2. Manually calling System.gc()
  3. When a particular threshold for the memory reaches, JVM triggers Garbage Collectors to avoid OutOfMemory error.
  4. G1 uses time based Garbage Collection. after certain intervals it Triggers Garbage Collectors.

Conclusion

Though Garbage Collection is automatically happens in Java but it is important for developers to know how it works to avoid mistake and make application more optimal and efficient.

If you like the article pease do follow me and subscribe to my newsletter to get the latest article updates from me.

--

--

Sumit Chaudhari

Programmer by heart. here to give back to the community.