You are here : Home / Core Java Tutorials / Series of JVM and Garbage Collection (GC) in java - Best explanations ever
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 >
- Young Generation
- Old Generation (tenured generation)
- Permanent Generation (tenured)
- D) Most important VM (JVM) PARAMETERS for Young, Old and Permanent Generation in JVM Heap memory>
- Young Generation (VM PARAMETERS for Young Generation) -
- Old Generation (tenured) (VM PARAMETERS for Old Generation)
- 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>
- Young Generation
- 1a) Eden,
- 1b) S0 (Survivor space 0)
- 1c) S1 (Survivor space 1)
- Old Generation (Tenured)
- Permanent Generation.
C) What are Young, Old and Permanent Generation in JVM Heap memory in java >
- 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).
- 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.
- 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>
- 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.
- 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.
- 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>
- Young Generation
- 1a) Eden,
- 1b) S0 (Survivor space 0)
- 1c) S1 (Survivor space 1)
- Old Generation (Tenured)
- Permanent Generation.
What are Young, Old and Permanent Generation in JVM Heap memory in java >
- 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.
- 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.
- 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>
- 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.
- Old Generation (tenured) (VM PARAMETERS for Old Generation)
-XX:NewRatio : NewRatio controls the size of young and old generation.
- 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>