Garbage Collection in Java

Garbage Collection in Java

Garbage collection is a mechanism to remove or destroy objects from memory when they are no longer needed in a program. Java garbage collector carry out the garbage collection process. Garabage collector runs as separate thread. Whenever it finds some CPU idle, it runs and performs the cleaning operations.

This process includes the following tasks :
  1. Java garbage collector keeps track of total number of references for the same object.
  2. Java garbage collector removes/destroys the object when it has no references.
  3. Java garbage collector invokes the finalize method.
By doing this process the memory occupied by the object previously can be allocated again.

Java has automatic garbage collection mesanism but still if we want to explicitly do it then the Java garbage collector can be involved in two ways :
  1. Using an instance of Runtime class and invoking the gc() method on it.
  2. Using the System.gc() method
Every application program in Java has a Runtime object associated with it, we can get the instace of this object by calling the static method getRuntime() of Runtime class. Syntax :
Runtime rt = Runtime.getRuntime();
Now that we have got the Runtime object, we can get various info such as the total amount of memory, currently available free memory by invoking various methods associated with this instance. Moreover we can invoke the garbage collector using this instance.

To know the total amount of memory allocated to JVM totalMemory() method is used which returns the same. To know the free JVM memory currently available use freeMemory() and to invoke the garbage collector gc() method is used.

The following program will give you a complete idea about how you can use these methods in order to perform garbage collection :
class JavaGarbageCollection {

public static void main(String[] args) {

	 Runtime rt = Runtime.getRuntime();
	 System.out.println("Total JVM Memory = " + rt.totalMemory());

	 System.out.println("Free memory before Garbage Collection = " + rt.freeMemory());

	 rt.gc();

	 System.out.println("Free memory after Garbage Collection = " + rt.freeMemory());

	}

}

/* Output of above code:-

Total JVM Memory = 264241152

Free memory before Garbage Collection = 262602144

Free memory after Garbage Collection = 7730200

*/
Note that these numbers will be different in your case.

Java 8 Garbage Collection

G1 Collector

G1 Garbage first Collector is designed for heaps with sizes bigger than 4 GB. G1 splits the heap into different regions. Each region can have a size of 1 MB to 32 MB depending on the heap size.

G1 collects regions containing garbage first based on the results from global marking phase. Global marking phase is used to know which objects are needed throughout the the heap. G1 can be enabled by using the flag - XX:+UseG1GC In Java 8, there is an optimization provided with G1 collector. This optimization us called String deduplication. In Java the character arrays that represent strings occupie much of the heap. This String deeduplication optimization enables the G1 collector to indentify string which are duplicated more than once across heap and modify them to point to the same internal char[] array. This avoids multiple copies of the same string residing in the heap unnecessarily. We can use the JVM argument XX:+UseStringDeduplication to enable this optimization.

G1 is the deafult Garbage collector in JDK 9.