Through this blog, I share my experiences, Problems that me and my team faced and how we resolved them.
Apart from that Technical tips, tricks, Java Basics and also questions that are asked in interviews related to Java.
Now blog can be accessed from a direct URL www.java-espresso.in

Monday, May 16, 2011

Heap structure in JVM

Following are the parts in JVM heap:

Young Generation

Eden Space

Survivor space

Virtual space

Tenured (Old) generation:

Virtual Space

Other Areas: Not a part of heap:

PermGen Area

Native Area

Take a look at the diagram for details(Perm Generation area and Native area are not shown):

JVM Heap details (Image from Oracle(SUN) itself):

Closer Look:Young Space: Young space holds the newly created objects and can be tuned with the parameter -XX:NewRatio=<value>, for example, you set -XX:NewRatio=3 [ which means ratio of young:old generation=1:3 and since total heap=young+old=4. So you have set the young space as one fourth of the total Heap] . You can even decide the initial and max sizes of the young space with parameters NewSize and MaxNewSize

Eden Space: All the objects either created inside the methods or as class member variables are 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. Objects that might never move to tenured generation are

Objects created inside the loops

Objects created inside the methods

Survivor Spaces:There are two survivor spaces. 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 and so this is the reason why it is called so. 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 spacee)+6]. If survivor space is too small to copy collections then survivor space will extend in the tenured area also. Virtual Space-1:It is the difference between the parameters of young space i.e, MaxNewSize-NewSize (Young Space Max Size - Young space initially size)

Tenured Generation: It contains the objects which have survived minor collections and are aged enough to be in tenured area. Major GC runs in the tenured area [See Collection algos of GC for collection algorithms of GC].Virtual Space-2:It is the difference between MaxHeapSize and IntialHeapSize(Virtual Space-2 = MaxHeapSize-IntialHeapSize)

Other Areas: PermGen(Permanent Generation) Area: It is the area where class loading and unloading happens. it stores metadata about classes , methods,etc. Please find the metadata term elaborated below:

Constant pool information

Array having references to methods

Internal Objects (for exceptions, etc.)

Information for compilers for optimization

Methods of class

Other Areas: Native Area:
Native area is for the internal operations of the JVM. JVM uses this area to load libraries and for intermediate code generation. We cannot size the native area but other areas can be sized to control it. We can calculate the native area as below:ProcessSize(Total memory allocated to Heap and other areas)-HeapSize-MaxPermSize
Collection Algorithms of GC (as per JVM 1.3, there are new additions as well):

Copying or Scavange : It is very efficient and used only for minor collections in Eden space as it requires significant footprint.

Mark-compact : it is slower than above technique. It does not require extra memory. It is used in major collections.

Incremental or train : this collector is used only if -Xincgc is passed on the command line. This is significantly slower than Mark-Compact

Other Related articles on this blog:JVM Memory Structure
References (Only which are worth reading :) ):