Thursday, May 28, 2015

Java Garbage Collection - Concepts -Part 3 - GC Types

Serial Collector:
Both young and old collections are done serially, in a stop-the-world fashion.

Young Generation:

  • Eden to one Survivor
  • From Survivor To Survivor
  • From Survivor Old generation
  • Survivor spaces swap roles (From becomes To and vice-versa)

Old Generation:

  • Old and Permanent generations are collected via a mark-sweep-compactcollection algorithm
  • Mark: Mark all Live objects
  • Sweep: Sweep them to another location, what’s left is garbage
  • Compact: Compact the freed up memory
  • After this, moving objects from Young generation to Old uses bump-the-pointer technique.

How to enable: -XX:+UseSerialGC

Default choice in Java 5
As of Java 6, to be used only in client applications –not recommended for server class machines or enterprise applications.

Parallel Collector :
Also known as Throughput Collector. Takes advantage of multiple CPUs

Young Generation: Parallel version of the Serial Collector Algorithm, still stop-theworld.

  • Eden to one Survivor
  • From Survivor To Survivor
  • From Survivor Old generation
  • Survivor spaces swap roles (From becomes To and vice-versa)

Old Generation: Same as Serial Collector

  • Old and Permanent generations are collected via a mark-sweep-compact collection algorithm
  • Mark:Mark all Live objects
  • Sweep: Sweep them to another location, what’s left is garbage
  • Compact: Compact the freed up memory
  • After this, moving objects from Young generation to Old uses bump-the-pointer technique.

How to enable: -XX:+UseParallelGC

Recommended for:

  • Machines with more CPUs
  • Applications that do not have pause time constraints (long pauses are OK); batch jobs

Parallel Compacting Collector:
Young Generation: Same as parallel collector
Old/Perm Generation: In Parallel, reduces pause times
  • Division into multiple regions followed by three phases
  • Mark Phase: Mark live objects in each region (Parallel)
  • Summary Phase: Identify where maximum space can be reclaimed by compaction
  • Compact Phase: Copy objects into region locations identified by summary phase.
How to enable: -XX:+UseParallelOldGC
Available from Java 5 update 6

Recommended for:
  • Machines with more CPUs
  • Not suitable for applications running on large shared machines, where no single application should monopolize several CPUs for extended periods of time.
  • On such machines, either decrease number of threads used for GC (-XX:ParallelGCThreads=n) or select a different collector.

Concurrent Mark-Sweep (CMS) Collector
Faster response Times
Observation: Young generation collections do not typically cause long pauses. Old generation collections, though infrequent, can impose long pauses, especially when large heaps are involved

Young Generation: Same as Parallel Collector

Old/Perm Generation: In Parallel, reduces pause times
  • Initial mark: Identify live objects reachable (STW)
  • Concurrent Mark: Mark all objects transitively reachable
  • Remark: Identify any modified objects during concurrent mark (STW)
  • Concurrent Sweep: Reclaim Garbage
Disadvantages: Larger Heap Sizes needed, Floating Garbage
How to enable: -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=n, -XX:+CMSIncrementalMode
Available from Java 5 update 6

Recommended for:
  • Application that needs shorter garbage collection pauses
  • Applications that have a relatively large set of long-lived data and that run on machines with two or more processors: web containers.
Java 8 and Improved G1 Collector

  • The Garbage first collector (G1) introduced in JDK 7 update 4 was designed to better support heaps larger than 4GB.
  • The G1 collector utilizes multiple background threads to scan through the heap that it divides into regions, spanning from 1MB to 32MB (depending on the size of your heap).
  • G1 collector is geared towards scanning those regions that contain the most garbage objects first, giving it its name (Garbage first).
  • There is a chance of the heap being depleted before background threads have finished scanning for unused objects, in which case the collector will have to stop the application which will result in a STW collection.
  • G1 also has another advantage that is that it compacts the heap on-the-go, something the CMS collector only does during full STW collections.
  • String deduplication: Since strings (and their internal char[] arrays) takes much of the heap, a new optimization has been made that enables the G1 collector to identify strings which are duplicated more than once across the heap and correct them to point into the same internal char[] array.
  • Metaspace: Classes & class loaders in permgen moved to MetaSpace. This is native memory and could grow up to the underlying infrastructure provided maximum. Classes and class loaders are no longer bound by heap space or permgen sizes.

Thursday, February 26, 2015

Java Garbage Collection - Concepts - Part 2

Young generation: 
Most objects are initially allocated in the young generation

  • -XX:MaxNewSize=NNN (Maximum size of Young Generation)
  • -XX:NewSize=NNN (Initial size of Young Generation)


Old generation:
Contains objects that have survived some number of young generation collections, as well as some large objects that may be allocated directly in the old generation


  • -XX:NewRatio=NNN (Size of Old Generation to Young Generation)
  • E.g., NewRatio=2 indicates old generation is 2/3 of total heap and young generation is 1/3 of the heap


Permanent generation: 
Holds objects that JVM uses, such as objects describing classes and methods, as well as the classes and methods themselves.

  • -XX:PermSize=NNN (Initial size of Perm Gen)
  • -XX:MaxPermSize=NNN (Maximum size of Perm Gen)

  • The young generation consists of an area called Eden plus two smaller survivor spaces
  • The majority of newly created objects are located in the Eden space.
  • After a GC in the Eden space, the objects are piled up into the Survivor space, where other surviving objects already exist.
  • Once a Survivor space is full, surviving objects are moved to the other Survivor space. Then, the Survivor space that is full will now empty.
  • The objects that survived these steps that have been repeated a number of times are moved to the old generation

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.