Thursday, October 14, 2010

G1(Garbage First) vs CMS Garbage collector

G1(Garbage First) vs CMS Garbage collector.

CMS garbage collector is garbage collector mechanism present in Java 6.In this garbage collector mechanism.The entire heap world is divided into two regions.
1.Young Generation heap
2.Old Generation heap(perm gen)
The young generation heap is again divided into 2 parts
1.Eden space 2.Survivor space
CMS(Concurrent mark and sweep algorithm) basically concentrates on the Young generation heap.
as in this space short lived objects are created which needs to be frequently garbage collected.
All objects in this space which are fit be to garbage collected are marked and concurrently sweeped and all those objects which may survive for longer time are either shifter to survivor space or Old generation heap example (Session objects,static variables).
As the entire heap is divided into 2 sets hence searching for dead object and memory defragmentation is little hectic for JVM as we know the heap size can be GB's.

So to ease the stress on JVM the Sun hotspot team in JAVA 7 has introduced a new mechanism for garbage Collection that is called G1(Garbage First).This approach is same as that Java 6 the only difference is the entire heap space is divided into a region of 1MB size except the Perm gen memory as they will be rarely touched the same is the case in Java 6.So here in G1 collector we can undestand from our basic instict that as heap is divided into regions of smaller size so marking and claming of dead Objects becomes easier.As JVM can run the gc in number of steps as a result the hold of JVM on memory will be for shorter time for each region unlike CMS in java 6 where JVM has to deal with memory for longer time.And we know that any secondary memory operations is time consuming.Hence we can see from here Java 7 will be faster than Java 6.But let us wait for it and test practically .