Garbage Collection in Java

The Garbage Collection feature of the java language is possibly its best feature. Thanks to garbage collection, programmers do not have to worry about memory allocation and dellocation since it automatically frees up memory for further use. It removes unused objects. It recognises objects that have not been referenced, and seeing as they are not in use, it removes them.

  • In C language, we can use malloc() or dealloc() to allocate the memory required. You can also clean that memory using these functions.

  • In C++ language, we can use new operators to allocate the memory and delete operator to clean the memory which is allocated by new operator.

  • In java language, we can allocate the memory using new operator. But we don't have any process of cleaning memory in java.

  • In java, automatic memory cleaning process was designed to reduce the developer’s burden, which is called garbage collection.

  • JVM is responsible to detect unused objects time to time.

Following are some of the criteria JVM uses to detect unused objects:

  1. When we assign null to the reference variable, then object referred by that reference variable is not referred or unused and is eligible for garbage collection Example:

       Hello h=new Hello ();
  2. When you assign one reference variable to another reference variable, then the object referred by the reference variable which is on the left side of the assignment operator is not referred and is eligible for GC

       Hello h1=new Hello(); 
       Hello h2=new Hello(); 

  • When the reference variable is out of scope, then the object referred by that reference variable is not referred or unused and is eligible for GC.

  • JVM uses the above mentioned said technique and identifies the unused object.When JVM detects a memory shortage problem, it invokes GC by handing over list of unused objects.

  • GC is a thread, which is mainly responsible for cleaning the memory of unused objects.

  • Sometimes unused objects may be refused by GC for cleaning the memory because they may hold the references to same resources like JDBC database connection, IO stream connection, printer connection, etc.

  • When you release the resources which are referred by some unused objects, then you can clean the memory of those unused objects without any trouble.

  • JVM invokes runFinalization() method of System class which is responsible for invoking Finalize() methods on all unused objects.

  • JVM works as following algorithm.

  • Detecting unused objects.

  • Prepares a list of unused objects

  • When memory shortage problem is detected, the JVM will do the following

    • calls runFinalization() method
    • calls gc() method
  • Cleans the memory of unused objects
    runFinalization() :- invokes the finalize() method on all unused objects finalize() method is of object class you need to override.

  • That is, override finalize () method in your class when you are using any resources in your class and write the resources cleanup code inside the finalize() method.