Heap Generations
In this tutorial, we are going to discuss about Heap Generations in java. Java objects are created in Heap and Heap is divided into three parts or generations for the sake of garbage collection in Java, these are called as Young generation, Tenured or Old Generation and Perm Area of the heap.
In garbage-collected programming languages like Java, the heap is divided into several generations, each serving a different purpose in the garbage collection process. These heap generations are organized based on the age of the objects allocated in the heap and their expected lifespan. The primary purpose of dividing the heap into generations is to optimize garbage collection efficiency by applying different garbage collection algorithms to each generation based on their characteristics.
1. Young Generation
Young generation is the place where all the new objects are created. When young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts – Eden Memory, Survivor 1 and Survivor 2 space. When an object first created in heap its gets created in new generation inside Eden space and after subsequent minor garbage collection if an object survives its gets moved to survivor 1 and then survivor 2 before major garbage collection moved that object to old or tenured generation. Important Points about Young Generation Spaces:
- Most of the newly created objects are located in the Eden memory space.
- When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.
- Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.
- Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation.
2. Old Generation
Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when it’s full. Old Generation Garbage Collection is called Major GC and usually takes longer time.
3. Permanent Generation
Permanent Generation or PermGen is a non-heap memory area here the Class Loading happens and the JVM allocates spaces for classes, class meta data, stores class level details, loading and unloading classes (e.g. JSPs), methods, String pool. java methods and the reference Objects here.
Perm Gen contains the metadata of the classes.
- Methods of a class (including the byte codes)
- Names of the classes (in the form of an object that points to a string also in the permanent generation)
- Constant pool(e.g String pool) information (data read from the class file).
- Object arrays and type arrays associated with a class (e.g., an object array containing references to methods).
- Internal objects created by the JVM (java/lang/Object or java/lang/exception for instance.
The PermGen is independent from the Heap Area. you can resize it according to the requirement using -XX:MaxPermSize and -XX:PermSize JVM Options. The Garbage collection happens in this area of JVM Memory as well. The Garbage collection in this area is called as “Class GC”. We can disable the Class Garbage Collection using the JVM Option -noclassgc. if ”-noclassgc” Java Option is added while starting the Server.it also holds all the reflective data of the virtual machine itself, stores class level details, loading and unloading classes (e.g. JSPs), methods, String pool.
Overall, heap generations is a crucial aspect of garbage collection in Java and other garbage-collected languages, allowing for more efficient memory management and better application performance.
That’s all about the Heap Generations in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!