Garbage Collection in java

Introduction to Garbage Collection in java

  • Garbage is nothing but unreferenced objects in java.
  • Garbage Collection in java is the process of identifying and removing the unused objects from the memory.
  • This process creates free space that further can be allocated to objects in need.
  • In C/C++ programming language, this process is handled by programmers through code. As human makes error,Β  the negligence in destruction of useless objects leads to insufficient memory.
  • In Java, Garbage Collection is automatic and the programmers doesn’t worry about destroying of useless objects. The Garbage Collector handles this process automatically.

Advantages of Garbage Collection

  • This process makes javaΒ memory efficient.
  • It is anΒ automatic process, no need for manual intervention.

How to make an object eligible for Garbage Collection in java

By following below ways an object can be unreferenced which in turns make the object eligible for Garbage Collection process :

  • Nullifying the reference :
Person obj1=new Person ();Β Β 

obj1=null;
  • Assigning a reference to other object makes first object unreferenced
Person obj1=new Person ();Β Β 

Person obj2=new Person ();Β Β 

obj1 = obj2;
  • Using Anonymous object
new Person ();

Some Important method to remember forΒ Garbage Collection

gc() method

The gc() method in Java is used to call the garbage collector to perform garbage collection process. The gc() method is present in System and Runtime classes.

publicΒ staticΒ voidΒ gc(){}

finalize() method

The finalize() method is called just before the object is garbage collected. This method can be used by developers for cleanup processing. This method is present in Object class.Β  Garbage collection is performed by a daemon thread -Garbage Collector(GC).

ThisΒ daemon thread calls the finalize() method just before the object is garbage collected soΒ you can use this method to for destroyingΒ  the remaining objects.

protectedΒ voidΒ finalize(){}

Please remember that neither finalization nor garbage collection is guaranteed.

Garbage Collector

Garbage Collector is the program in java, which runs in the background and it looks into all the objects in the memory and find out unreferenced objects . All these unreferenced objects are deleted from the memory and space is used for allocation to other objects.

Garbage collector is one of the example ofΒ Daemon threadΒ as it runs in background.

Simply saying, Garbage Collector performs the Garbage Collection process.

Java Garbage Collection processing

As we know, Garbage collection in java is an automatic process. The developer doesn’t required to mark the objects explicitly to be deleted.

The Garbage Collection process in Java is handled by daemon thread which lives in the JVM. Garbage Collection process can be implemented with each JVM , However the requirement is to meet the JVM specification.

Oracle’s HotSpot is by far the most common JVM provided by java. It provides various robust and mature garbage collection options.

HotSpot has multiple garbage collectors in place and those are surely optimum for variety of use cases. HotSpot’s each garbage collector follow the same basic process.Β  Steps are simplified as below :

Step 1 :Β  Marking of all unreferenced objects

The Garbage collector in its very first step identifies which are all the objects which are unreferenced i.e not in use.

Step 2 :Β  Normal deletion of marked objects

The garbage collector in second step, removes/deletes the marked objects and reclaims the free up space which can be further used for allocation of new objects.

Step 2 :Β  Memory Compaction

There may be the case whereΒ free spaces at different locations in the heap is left in small chunks after deletion of unreferenced / marked objects. SoΒ  garbage collector performs the memory compaction process to to avoid memory fragmentation . ThisΒ Β process makes the memory allocation process much easy.

Each Garbage collector of HotSpot implements a general strategy of garbage collection which categorizes objects by their age.Β The heap in Java is divided intoΒ three sections :

Sections of Java Heap

garbage collection in java

Young Generation

  • Firstly The newly created objects considered to be in the Young Generation.
  • The Young Generation’s further subdivided into an
    • Eden space :Β  Here all new objects start.
    • Two Survivor spaces :Β  Here objects are moved from Eden space after surviving one garbage collection cycle.
  • It is called as minor garbage collection event when objects are garbage collected from the Young Generation.

Old Generation

  • Objects that are long-lived are eventually moved from the Young Generation to the Old Generation.
  • It is called as major garbage collection event when objects are garbage collected from the Old Generation.

Permanent Generation

  • Permanent Generation stores the classes and methods i.e. MetaData only.
  • Classes which are no longer being used may get garbage collected from the Permanent Generation.

Types of Java Garbage Collectors

Serial GC:

In Serial GC, all garbage collection events are conducted in one thread serially. Additionally, After each garbage collection process, compaction is executed .

Parallel GC:

In Parallel GC, For minor garbage collection process, multiple threads gets used. For major garbage collection process and old generation compaction, single thread gets used.

CMS (Concurrent Mark Sweep) GC:

In CMS GC, For minor garbage collection process, multiple threads are used. It uses the same algorithm as Parallel. CMS Gc’s major garbage collection is handled by multiple threads same as Parallel Old. Additionally, No compaction is performed in CMS GC. During major garbage collection process, CMS runs concurrently alongside the processes just to minimize critical events such as when the garbage collector process stops the application).

G1 (Garbage First) GC:

This is the newest garbage collector and it is intended as a replacement for CMS GC. It is also concurrent and parallel like CMS GC but it works quite differently compared to the old garbage collectors.

Conclusion

That’s all folks! In this article, you have got the complete overview Java Garbage Collection , Its advantages , How to make Objects eligible for Garbage Collection process, Java Garbage Collection Processing, What is Garbage Collector and Its types and Some important methods with reference to Garbage Collection.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply