Java Garbage Collection

This article discusses about Java Garbage Collection (GC) which is considered as one of the complex topics in Java programming language.

As the name suggests, garbage collection in java deals with searching, discovering and deleting garbage from memory automatically without having to do the job explicitly by the users. Here garbage refers to unreferenced objects.

There are various ways by which an object can turn out to be unreferenced and useless. Some are:

  • Nulling of the reference
  • Assigning the reference to another
  • Anonymous object and many more

So the garbage collection feature finds out these objects and deletes them from memory automatically and deletes them which results in efficient memory use and management.

If you were to do the same garbage collection and optimization in C language then you would use free() function whereas in C++ you would have used delete() method. So there is automation of this process in Java which leads to lesser hassle for the users to deal with.

To phrase it technically, Java garbage collection deals with tracking every object in the JVM (Java Virtual Machine) heap space and removes (deletes/de-allocates) the unused ones.




There are 4 types of Garbage Collection (GC) implementations:

  1. Serial Garbage Collector
  2. Parallel Garbage Collector
  3. CMS Garbage Collector
  4. G1 Garbage Collector

 

Serial Garbage Collector

This implementation of GC works with a single thread and freezes all application threads when it is run. So it is not the best option for use in multi-threaded applications. All the garbage collection events are conducted serially in one thread.

It is used in applications that do not have small pause time requirements and run on client-style machines. It is designed for single threaded systems and is preferred for small heap sizes in the JVM.

 

Parallel Garbage Collector

This variation is the default Garbage Collector of JVM and is also referred to as Throughput Collectors. Parallel GC makes use of multiple threads while managing the heap which was not the case with Serial GC. However, like Serial GC it also freezes rest of the application threads while it is run.

Specification of maximum garbage collection threads and pause time, throughput and footprint (heap size) can be done.

 

CMS Garbage Collector

CMS stands for concurrent mark sweep and uses multiple GC threads concurrently to scan through the heap and mark the unreferenced objects which are later deleted/de-allocated in the sweep.  It is preferred for those applications that require short garbage collection pauses and can afford to share resources with the GC. Applications using this implementation of the GC are comparatively slower than those not using one but they do not pause the entire applications entirely while performing garbage collection.

This implication of garbage collector goes into Stop-The-World (STW) mode in two cases:

  • While initializing the initial marking of roots
  • When the application has changed the state of the heap while the algorithm was running concurrently; forcing it to go back to make sure the right objects are marked.

A promotion failure is encountered when an object is moved from the younger generation to the old generation and the collector did not have enough time to make space in the old generation.

G1 Garbage Collector

G1 which stands for Garbage First GC is made for applications that run on multi-processor machines with large memory. Compared to the CMS collector, it is more performance efficient.

G1 GC partitions the heap into a set of equal sized heap regions and marks a concurrent global marking phase to determine whether the objects in the heap are being used or not.

After the marking is done, which regions are mostly empty is known by the GC. The GC then collects garbage from these areas first (which is known as the sweeping phase) resulting in large amount of free space. Hence the name comes as garbage first.

Benefits of Garbage Collection:

  • No manual memory allocation/de-allocation handling as unused memory space is automatically handled by GC
  • No overhead of handling Dangling Pointer
  • Automatic Memory Leak management

Disadvantages of Garbage Collection:

  • Keeping track of object reference creation/deletion requires more CPU power and may affect the performance of requests which require large memory
  • Programmers have no control over the scheduling of CPU time dedicated to freeing objects that are no longer needed
  • Using some GC implementations might result in application stopping unpredictably
  • In some cases, automatic memory management may not be as efficient as proper manual memory allocation/de-allocation

 

A simple program showing an implementation of Java Garbage Collection:

public class TryingGrabageCollection{  
        public void finalize(){
                System.out.println("Object has been collected by the garbage collector!");
        }  
 	public static void main(String args[]){  
  		TryingGrabageCollection obj1 = new TryingGrabageCollection ();  
  		TryingGrabageCollection obj2 = new TryingGrabageCollection ();  
  		obj1 =null;  
  		obj2 =null;  
  		System.gc();  
        }  
} 

Output:

Object has been collected by the garbage collector!
Object has been collected by the garbage collector!

 

Leave a Reply

avatar