Article provenance: http://ifeve.com/jvm-yong-generation/
Talk about the young generation of JVM
1.Why is there a young generation
Why do we need to stack up for generations? Is it impossible for him to do what he does? In fact, the only reason for generation is that it can optimize GC performance. You first think, if there is no generation, then all of our objects are in one piece. When GC, we have to find which objects are useless.This will scan all the areas of the heap. And many of our objects are alive and dead. If they are divided, we put the newly created objects to a certain place. When GC, we reclaim the area of the “dead” object, which will make room for a lot of space.
2.GC in the young generation
HotSpot JVMThe younger generation is divided into three parts: 1 Eden districts and 2 Survivor districts (from and to respectively). The default ratio is 8:1. Why is this ratio default? Next we will talk about it. In general, newly created objects are allocated to the Eden area (some big pairs).Like special processing, these objects will be moved to the Survivor area if they remain alive after the first Minor GC. When Survivor Minor is GC, the age will increase by 1 years. When its age increases to a certain degree, it will increase.It is moved to the old generation.
Because the young generation of objects is basically dead (more than 80%), so the garbage collection algorithm used in the young generation is a copy algorithm. The basic idea of the replication algorithm is to divide the memory into two blocks and use one of them each time. When the memory is used out, the living object is copied to another block.Above。 The replication algorithm does not produce memory fragments.
At the beginning of GC, the object will only exist in the Eden area and the Survivor area named “From”, and the Survivor area “To” is empty. Immediately after GC, all the surviving objects in the Eden area will be copied to “To” and in the “From” area.The surviving objects will decide where to go according to their age. Objects of age reaching a certain value (age threshold, which can be set by -XX:MaxTenuringThreshold) will be moved to the older generation, and objects that do not reach the threshold will be copied to the “To” area.. After this GC, the Eden area and the From area have been emptied. At this time, “From” and “To” will exchange their roles, that is, the new “To” is the last “From” before the GC, and the new “From” is the last “To” before the GC. No matter whatIt will ensure that the Survivor area named To is empty. Minor GC will repeat this process all the time until the “To” area is filled, and when the “To” area is filled, it will move all the objects to the old generation.
3.The whole life of an object
I am an ordinary Java object, I was born in the Eden area, and in the Eden area I also saw a very similar little brother with me, and we played for a long time in the Eden area. One day, there were so many people in Eden district that I was forced to go to From in Survivor district.”Since I went to the Survivor area, I began to drift, sometimes in the” From “area of Survivor, sometimes in the” To “area of Survivor. By the time I was 18 years old, my father had said that I had grown up and had rushed into society. So I amI went to the old age, there are many people in my age, and I am quite old. I know a lot of people here. In my old age, I lived for 20 years (GC plus one year old), and then I was recycled.
4.On the JVM parameters of the young generation
It is used to set the size of the younger generation. It is recommended to set 1/3 or 1/4 for the whole heap size. The two values are set as large.
It is also important to set the ratio of Eden to one of Survivor.
This parameter is used to show the size of objects in each age group in Survivor area at Minor GC.
It is used to set the minimum and maximum age of objects that are promoted to the old age. When each object persists once Minor GC, the age is increased by 1.