Monday, 21 September 2015

Generational Garbage Collection

Why It needed?

      1.    Most objects die young.

      2.    Over 90% garbage collected in a GC is newly created post the previous GC cycle.

Mark and compact all the objects in a JVM is inefficient. As more and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time. However, empirical analysis of applications has shown that most objects are short lived.

Generational garbage collection is tracing garbage collection that makes use of the generational hypothesis. Objects are gathered together in generations. New objects are allocated in the youngest or nursery generation, and promoted to older generations if they survive. Objects in older generations are condemned less frequently, saving CPU time.

JVM Generations

YoungGen : It is place where lived for short period and divided in two parts:

à Eden Space(heap) : When object created using new keyword memory allocated on this space.

àSurvivor Space(heap) : The pool containing objects that have survived the garbage collection of the Eden space.

Tenured Generation(heap) : This memory pool contains objects which survived after multiple garbage collection means object which survived after garbage collection from Survivor space.
   à OldGen : This pool is basically contain tenured and virtual (reserved) space and will be holding those objects which survived after garbage collection from YoungGen space.

Permanent Generation : This memory pool contain permanent class metadata and descriptors information so PermGen space always reserved for classes and those that is tied to the classes for example static members.

Java8 Update: PermGen is replaced with Metaspace which is very similar.
Main difference is that Metaspace re-sizes dynamically i.e., It can expand at runtime.
Java Metaspace space: unbounded (default).

Code Cache (Virtual or reserved) : If you are using HotSpot Java VM this includes code cache area that containing memory which will be used for compilation and storage of native code.

The information gathered from the object allocation behavior can be used to enhance the performance of the JVM. Therefore, the heap is broken up into smaller parts or generations.

Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.

Old Generation is used to store long surviving objects. A threshold is set for young generation object, if object survived more meet the threshold then object gets moved to the old generation. This event is called a major garbage collection.

Major garbage collection are also Stop the World events. Often a major collection is much slower because it involves all live objects.

Major garbage collections should be minimized. The length of the Stop the World event for a major garbage collection is affected by the kind of garbage collector that is used for the old generation space.

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.

Object in different generations

First, any new objects are allocated to the Eden space. Both survivor spaces start out empty.


When the Eden space fills up, a minor garbage collection is triggered.

Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the Eden space is cleared.

At the next minor GC, the same thing happens for the Eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space.

At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.

This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.

As minor GCs continue to occur objects will continue to be promoted to the old generation space.


So that pretty much covers the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...