Heap Area in JVM

Heap stores all the objects and arrays that are created by the java program.Every JVM instance has only one heap area and is shared by all threads running inside the JVM instance.

The heap is divided into two generations: The Young Generation and the Tenured(Old) Generation.We can set the size of Young Generation by using -Xmn option in VM args.

Heap Area in JVM

  1. Young Generation
    • Eden Space
    • Survivor space
  2. Tenured (Old) generation

Young Generation

  1. Eden Space:All new objects are first created in the Eden Space.Space allocated to objects created inside the methods is reclaimed after the method execution is over. Some objects might live longer as reference pointing to them are class member variables. If the Eden space starts filling up then minor garbage collector runs and shifts the old objects to the Tenured Generation space so that space is available for creation of new objects in Eden space.
  2. Survivor Spaces: Young generation has two Survivor spaces. One survivor space is empty at any given time.Before objects are being moved to Tenured space, objects which are still in use, survive the minor GC for first few times by keep on juggling between these survivor spaces. Referenced objects from the ‘eden’ and ‘from’ into the ‘to’ survivor space. Once the minor GC is over, the ‘from’ and ‘to’ roles (names) are swapped.
    Survivor Ratio: it is the ratio of the Eden space to Survivor space in the Young space. This parameter can be controlled by property -XX:SurvivorRatio. Suppose if the -XXSurvivorRatio=6, it means each survivor space is one eigth of the total Young space[total young space=2*1 (as there are 2 survivor spaces)+6].     

Tenured Generation

Depending on the threshold limits by age, objects are moved from the Survivor spaces to the Tenured space. ‘Age’ is the number of times that it has moved within the survivor space.Major GC runs in the tenured area.Please visit Garbage Collector(GC) for more details about major GC.

Related Topics

Java Virtual Machine(JVM)

JVM Interview Questions

Method Area(Non-Heap Area)

Leave a Reply

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