Skip to main content

Garbage Collection in JAVA


Java has a very good memory management system. When an object in Java is no longer useful, or we can say that we will not require that object in the future, it is destroyed. The memory space is now available for any other purpose that was previously occupied. The Garbage Collector in Java handles the entire process.

Garbage collection (or GC) is a method of reclaiming memory that is no longer in use for reuse. Unlike other languages that require manual allocation and destruction of objects, Java programmers do not need to pick up and study each object to determine whether it is required.

The garbage collector is the perfect example of a Daemon thread because it runs in the background all the time.

DAEMON THREAD

A daemon thread is a low-priority thread that performs operations such as garbage collection in the background.

Properties:

  • The daemon status of a newly generated thread is inherited by it. Because the main thread is non-daemon, all threads formed within it (children of the main thread) are non-daemon by default. Using the setDaemon() method of the thread class, you can convert a user thread to a Daemon.
  • public void setDaemon(boolean status): This method is used to determine whether or not current is a daemon. If the thread is Daemon, it returns true; otherwise, it returns false.

Daemon VS User Thread

  • Thread Priority: Because user threads have a higher priority than daemon threads, they won't obtain as much CPU as a daemon thread.
  • Creation of Thread: The application normally creates a user thread to do multiple tasks at the same time. Daemon threads, on the other hand, are typically created by JVM for garbage collection purposes.
  • Termination of Thread: If all user threads have completed their execution but the user thread is terminated by the program or by itself, the JVM will cause the daemon thread to end. A user thread can continue to execute while the JVM is executing, but a daemon thread cannot. This is the most important distinction between user and daemon threads.

In Java, there are two sorts of garbage collection activities that typically occur:

  1. When inaccessible objects in the young generation heap memory are deleted, a minor or incremental garbage collection occurs.
  2. When the items that survived the minor garbage collection and were copied into the old generation or permanent generation heap memory are eliminated, it is said that a large or full garbage collection has occurred.

How to make an object GC-eligible:

  • Even while the programmer is not responsible for destroying unnecessary things, it is highly suggested that if an object is no longer required, it be made inaccessible (thus eligible for GC).
  • In general, there are four possible approaches to make an object garbage-collectible.
  1. Nullifying the reference variable
  2. Re-assigning the reference variable
  3. Object created inside method
  4. Islands of Isolation

Island of Isolation(IOI)

The Garbage Collector module in Java handles object destruction, and objects with no references are eligible for garbage collection. This type of object can be identified by Garbage Collector.

Object 1 references Object 2 and Object 2 references Object 1. Neither Object 1 nor Object 2 is referenced by any other object. That’s an island of isolation.


To free up memory, the JVM must halt the application for at least a short period of time and do garbage collection. The procedure is known as "stop-the-world." This means that all threads will cease running except the GC threads until the GC threads have completed and the garbage collector has freed up all objects.

Modern garbage collection solutions strive to avoid halting "stop-the-world" delays by completing as much work as feasible in the background (i.e. on a separate thread), such as identifying unreachable garbage instances while the application process is still running.

Garbage collection uses CPU resources to determine which memory should be freed. Various garbage collectors have been created over time to reduce application interruptions caused by garbage collection while also reducing the performance hit associated with garbage collection.

Why is Monitoring Garbage Collection Important?

Garbage collection can have an unpredictably negative impact on the performance of a Java application. GC activity adds a lot of CPU strain and slows down application operation when it happens frequently. As a result, business transactions take longer to complete, affecting the user experience of end-users accessing the Java application.

A memory leak in the Java application can cause excessive garbage collection activities. Increased garbagecollection activity might also be caused by insufficient memory allocation to the JVM. And when the JVM's garbagecollection activity is excessive, it frequently manifests as increased CPU consumption!


Summary:

Is garbage collection good or bad?

 Definitely a plus. However, as the saying goes, too much of anything is bad. As a result, you must ensure that Java heap memory is appropriately set up and managed in order to optimize GC activity.

When garbage collection is needed?

It's required when unreferenced objects need to be removed. Because this isn't a manual task, the JVM will take care of it automatically. You should have figured out why and when GC is required based on the preceding information. So that's the solution to your inquiry.

How to tune garbage collection? 

There are two common methods for doing so:
  1. Reduce the number of objects sent to the old generation area as much as possible.
  2. Set the major (or entire) GC time to be as short as possible.
















Comments