What are Young, Old (tenured) and Permanent Generation in JVM in java




In this core java tutorial we will learn What are Young, Old and Permanent Generation in JVM in java.


Contents of page >
  • A) JVM Heap memory (Hotspot heap structure) with diagram in java >
  • B) JVM Heap memory (Hotspot heap structure)  in java consists of following elements>
  • C) What are Young, Old and Permanent Generation in JVM Heap memory in java >
  1. Young Generation
  2. Old Generation (tenured generation)
  3. Permanent Generation (tenured)
  • D) Most important VM (JVM) PARAMETERS for Young, Old and Permanent Generation in JVM Heap memory>
  1. Young Generation (VM PARAMETERS for Young Generation) -
  2. Old Generation (tenured) (VM PARAMETERS for Old Generation)
  3. Permanent Generation (VM PARAMETERS for Permanent Generation) .

Before learning about Young, Old (tenured) and Permanent Generation in java we must know about JVM Heap memory (Hotspot heap structure).

A) JVM Heap memory (Hotspot heap structure) with diagram in java >

B) JVM Heap memory (Hotspot heap structure)  in java consists of following elements>
  1. Young Generation
    • 1a) Eden,
    • 1b) S0 (Survivor space 0)
    • 1c) S1 (Survivor space 1)
  2. Old Generation (Tenured)
  3. Permanent Generation.

C) What are Young, Old and Permanent Generation in JVM Heap memory in java >


  1. Young Generation


New objects are allocated in Young generation.


Young Generation consists of >
    • 1a) Eden,
    • 1b) S0 (Survivor space 0)
    • 1c) S1 (Survivor space 1)


Young Generation is further divided into three parts Eden, S0 (Survivor space 0) and S1 (Survivor space 1).


Minor garbage collection occurs in Young Generation.


When minor garbage collection?
When the young generation fills up, this causes a minor garbage collection.
All the unreferenced (dead) objects are cleaned up from young generation.


When objects are moved from young to old generation in JVM heap?
Some of the objects which aren't cleaned up survive in young generation and gets aged.  Eventually such objects are moved from young to old generation.


What is Stop the World Event?
Minor garbage collections are called Stop the World events.
All the non-daemon threads running in application are stopped during minor garbage collections (i.e. the application stops for while).
Daemon threads performs minor garbage collection. (Daemon threads are low priority threads which runs intermittently in background for doing garbage collection).


  1. Old Generation (tenured generation)
The Old Generation is used for storing the long surviving aged objects (Some of the objects which aren't cleaned up survive in young generation and gets aged.  Eventually such objects are moved from young to old generation).


Major garbage collection occurs in Old Generation.


At what time (or what age) objects are moved from young to old generation in JVM heap?
There is some threshold set for young generation object and when that age is met, the object gets moved to the old generation.


What is major garbage collection in java?
When the old generation fills up, this causes a major garbage collection. Objects are cleaned up from old generation.
Major collection is much slower than minor garbage collection in jvm heap because it involves all live objects.


Major garbage collection are Stop the World Event in java?
Major garbage collections are also called Stop the World events.
All the non-daemon threads running in application are stopped during major garbage collections.
Daemon threads performs major garbage collection.


Major garbage collections in responsive applications in java?
Major garbage collections should be minimized for responsive applications because applications must not be stopped for long.


Optimizing Major garbage collections in responsive applications in java?
Selection of appropriate garbage collector for the old generation space affects the length of the “Stop the World” event for a major garbage collection.

  1. Permanent Generation.


Permanent generation Space contains metadata required by JVM to describe the classes and methods used in the application.


The permanent generation is included in a full garbage collection in java.


The permanent generation space is populated at runtime by JVM based on classes in use in the application.


The permanent generation space also contains Java SE library classes and methods in java.


JVM garbage collects those classes when classes are no longer required and space may be needed for other classes in java.




D) Most important VM (JVM) PARAMETERS for Young, Old and Permanent Generation in JVM Heap memory>

  1. Young Generation (VM PARAMETERS for Young Generation) -


-Xmn : -Xmn sets the size of young generation.


-XX:NewRatio : NewRatio controls the size of young generation.
Example of using -XX:NewRatio, -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.

-XX:NewSize - NewSize is minimum size of young generation which is allocated at initialization of JVM.
Note : If you have specified -XX:NewRatio than minimum size of the young generation is allocated automatically at initialization of JVM.


-XX:MaxNewSize - MaxNewSize is the maximum size of young generation that JVM can use.

-XX:SurvivorRatio :   (for survivor space)
SurvivorRatio can be used to tune the size of the survivor spaces, but this is often not as important for performance.
Example of using -XX:SurvivorRatio > -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6.
In other words, each survivor space will be one eighth of the young generation (not one seventh, because there are two survivor spaces).


What if survivor spaces are too small?
If survivor spaces are too small, copying collection overflows directly into the tenured generation.


What if survivor spaces are too large?
If survivor spaces are too large, they will be uselessly empty. At each garbage collection the virtual machine chooses a threshold number of times an object can be copied before it is tenured. This threshold is chosen to keep the survivors half full. -XX:+PrintTenuringDistribution can be used to show this threshold and the ages of objects in the new generation. It is also useful for observing the lifetime distribution of an application.


  1. Old Generation (tenured) (VM PARAMETERS for Old Generation)
-XX:NewRatio : NewRatio controls the size of young and old generation.
Example of using -XX:NewRatio, -XX:NewRatio=3 means that the ratio between the young and old/tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.


  1. Permanent Generation (VM PARAMETERS for Permanent Generation) .


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.


-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.
Examples of using -XX:PermSize VM (JVM) option in java >
Example1 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=512m It will set initial value of Permanent Space as 512 megabytes to JVM


Example2 of using -XX:PermSize VM (JVM) option in java >
java -XX:PermSize=1g It will set initial value of Permanent Space as 512 gigabyte to JVM

-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


Examples of using -XX:MaxPermSize VM option in java >
Example1 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=512m It will set maximum value of Permanent Space as 512 megabytes to JVM


Example2 of using -XX:MaxPermSize VM (JVM) option in java >
java -XX:MaxPermSize=1g It will set maximum value of Permanent Space as 1 gigabyte to JVM

What is the maximum perm gen size value you can set on your system, if you aren’t sure about the system configurations?
Try out for -XX:MaxPermSize=256m, if it works then try -XX:MaxPermSize=512m, if it works then try -XX:MaxPermSize=1024m and so on. Be cautious before going beyond 8g.


Summary -


I am highlighting all the summary portion to keep it separate from whole tutorial. Please refer above for complete detail.


JVM Heap memory (Hotspot heap structure)  in java consists of following elements>
  1. Young Generation
    • 1a) Eden,
    • 1b) S0 (Survivor space 0)
    • 1c) S1 (Survivor space 1)
  2. Old Generation (Tenured)
  3. Permanent Generation.

What are Young, Old and Permanent Generation in JVM Heap memory in java >


  1. Young Generation


New objects are allocated in Young generation.


Minor garbage collection occurs in Young Generation.


When minor garbage collection?
When the young generation fills up, this causes a minor garbage collection.
All the unreferenced (dead) objects are cleaned up from young generation.


  1. Old Generation (tenured generation)
The Old Generation is used for storing the long surviving aged objects.


Major garbage collection occurs in Old Generation.


When the old generation fills up, this causes a major garbage collection. Objects are cleaned up from old generation.


  1. Permanent Generation.


Permanent generation Space contains metadata required by JVM to describe the classes and methods used in the application.


The permanent generation is included in a full garbage collection in java.


Most important VM (JVM) PARAMETERS for Young, Old and Permanent Generation in JVM Heap memory in java>


  1. Young Generation (VM PARAMETERS for Young Generation) -


-Xmn : -Xmn sets the size of young generation.


-XX:NewRatio : NewRatio controls the size of young generation.

-XX:NewSize - NewSize is minimum size of young generation which is allocated at initialization of JVM.


-XX:MaxNewSize - MaxNewSize is the maximum size of young generation that JVM can use.


-XX:SurvivorRatio :  (for survivor space)
SurvivorRatio can be used to tune the size of the survivor spaces.


  1. Old Generation (tenured) (VM PARAMETERS for Old Generation)
-XX:NewRatio : NewRatio controls the size of young and old generation.


  1. Permanent Generation (VM PARAMETERS for Permanent Generation) .


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.


-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.


-XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.

-XX:MaxPermSize: It’s maximum value of Permanent Space that JVM can allot up to.

So in this core java tutorial we learned What are Young, Old and Permanent Generation in JVM in java.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


 


Different type of garbage collectors in java>

>Serial collector / Serial GC (Garbage collector) in java

>Throughput GC (Garbage collector) or Parallel collector in java

>Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java

>G1 garbage collector / Garbage first collector in java

>PS Scavenge and PS MarkSweep


JVM in detail - Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java



Minor, Major and Full garbage collection >

>What are Minor, Major and Full garbage collection in JVM in java





Important VM parameters >

>Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java

>What are -Xms and -Xmx JVM parameters in java, And differences between them with examples


>-Xmn JVM parameters in java with examples - Setting young generation size

eEdit
Must read for you :