Home » Core Java » Garbage Collection in Java

Garbage Collection in Java

What is Garbage collection :

1 : Garbage collection (GC) is a form of automatic memory management.Garbage collection was invented by John McCarthy around 1959 to abstract away manual memory management.
2 : Garbage Collection in Java is carried by a daemon thread called Garbage Collector.Garbage collector attempts to reclaim memory occupied by objects that are no longer in use by the program i.e. objects that are not reachable from any live threads or by any static references.
3 : System.gc() and Runtime.gc() methods are used to send request of Garbage collection to JVM,but we can not guaranty that garbage collection will happen by invoking these methods.Garbage collection thread invokes finalize() method of Object class before completely removing an object from memory to  gives an opportunity to perform final  resource cleanup.

4 : Java Heap is divided into 3 parts or generations known as Young or New generation, Tenured or Old Generation and Perm Generation Area of heap. New Generation is further divided into 3 parts known as Eden space, Survivor 1 and Survivor 2 space. When an object first created in heap, its gets created in new generation inside Eden space and after subsequent minor garbage collection if object survives its gets moved to survivor 1 and then survivor 2 before major garbage collection moved that object to old or tenured generation.String pool is created in PermGen area of Heap.This  heap architecture is according to Java 1.6 version.Please see my tutorial on latest java 1.8 version.Application’s performance,efficiency and throughput are affected during major garbage collection for some time.

Advantages :

1 : Programmer does not need to take care of garbage collection.Based on used Java heap size ,JVM automatically does garbage collection whenever required.
2 : Garbage collection makes memory efficient by reclaiming memory occupied by unreferenced objects .This helps in trouble shooting java.lang.OutOfMemoryError.

Eligible For garbage collection :

1 : All references of the object  are explicitly set to null.

2 : By assigning a reference to another reference.if an object holds reference of another object and when you set    first  or parent object’s reference to null, child or second object becomes eligible for garbage collection.

3 : Object is created inside a block or method and reference goes out scope once control exit from that block.
4 : Any object which  has only live weak references via WeakHashMap it will be eligible for garbage collection.WeakHashMap is an implementation of the Map interface that stores only weak references to its keys. Storing only weak references allows a key-value pair to be garbagecollected when its key is no longer referenced outside of the WeakHashMap.
5 : By anonymous object creation.

 

The Mark and Sweep Model :

1 : JVM uses the mark and sweep algorithm for  performing garbage collections of the whole heap.During the mark phase all objects that are reachable from Java threads, native handles and other root sources are marked as alive, as well as the objects that are reachable from these objects and so forth. This process identifies and marks all objects that are still used, and the rest can be considered garbage.During the sweep phase the heap is traversed to find the gaps between the live objects.These gaps are recorded in a free list and are made available for new object allocation.

2 : Mark and sweep algorithm can be divided or can be a combination of  mostly concurrent mark and sweep and  parallel mark .The parallel mark and sweep strategy (also called the parallel garbage collector) uses all available CPUs in the system for performing the garbage collection as fast as possible. All Java threads are paused during the entire parallel garbage collection.

The mostly concurrent mark and sweep strategy allows the Java threads to continue running during large portions of the garbage collection. The threads must however be stopped a few times for synchronization.The mostly concurrent mark phase is divided into four parts:

Initial marking, where the root set of live objects is identified. This is done while the Java threads are paused.
Concurrent marking, where the references from the root set are followed in order to find and mark the rest of the live objects in the heap. This is done while the Java threads are running.
Precleaning, where changes in the heap during the concurrent mark phase are identified and any additional live objects are found and marked. This is done while the Java threads are running.
Final marking, where changes during the precleaning phase are identified and any additional live objects are found and marked. This is done while the Java threads are paused.
The mostly concurrent sweep phase consists of four parts:

Sweeping of one half of the heap. This is done while the Java threads are running and are allowed to allocate objects in the part of the heap that isn’t currently being swept.
A short pause to switch halves.
Sweeping of the other half of the heap. This is done while the Java threads are running and are allowed to allocate objects in the part of the heap that was swept first.
A short pause for synchronization and recording statistics.


Leave a comment

Your email address will not be published. Required fields are marked *

one × 4 =