Tuesday, January 6, 2015

Java Garbage Collection -Concepts : Part 1

In a programming language like C, allocating and de allocating memory is a manual process and dynamic memory is allocated using an explicit allocate/free model ( malloc/calloc–free)

Practical issues : Passing this object around, developers lose track of the object; fail to free up memory (Memory Leaks)

Modular programming difficult: Determining free points across module boundaries is nearly impossible without explicit and hard-to-understand cooperation between modules

In Java, process of de allocating memory is handled automatically by the garbage collector.

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. The basic process can be described as follows

The garbage collector was created based on the following two hypotheses.

  • Most objects soon become unreachable.
  • References from old objects to young objects only exist in small numbers.

In order to preserve the strengths of this hypothesis, it is physically divided into two -young generation and old generation.

Young generation collections occur relatively frequently and are efficient and fast because the young generation space is usually small and likely to contain a lot of objects that are no longer referenced.

Objects that survive some number of young generation collections are eventually promoted, or tenured, to the old generation.

Old generation is typically larger than the young generation and its occupancy grows more slowly. As a result, old generation collections are infrequent, but take significantly longer to complete.

The garbage collection algorithm chosen for a young generation typically tuned for speed, since young generation collections are frequent.

Old generation is typically managed by an algorithm that is more space efficient, because the old generation takes up most of the heap and old generation algorithms have to work well with low garbage densities


Options With GC Algorithms

Parallel Vs Serial :
A garbage collector algorithm can be parallel or serial. In former case if a system has multiple cores the collection work is divided and ran on different core. In case of serial, even if machine has multiple cores, only one core will be used for collection.

Concurrent Vs Stop the World :
In case of stop the world, the application is stopped while collection whilein case of concurrent algorithm collection is done with application running.
In case of concurrent, collection happens on objects which may get updated while collecting, hence they add some extra overheads and require a bigger heap size.

Compacting Vs Non-Compacting:

Once garbage collection is done, GC may or may not compact the memory. Moving the live objects to one end of memory creates a free pool of memory at other end. It's easy and fast to allocate memory with one free end. Non compacting GC algorithm are fast but it causes memory fragmentation and slow down the allocation.